blob: c60649ec119329f5e5b17ccc2306eab4f869f838 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * PACKET - implements raw packet sockets.
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
11 *
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090012 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Alan Cox : verify_area() now used correctly
14 * Alan Cox : new skbuff lists, look ma no backlogs!
15 * Alan Cox : tidied skbuff lists.
16 * Alan Cox : Now uses generic datagram routines I
17 * added. Also fixed the peek/read crash
18 * from all old Linux datagram code.
19 * Alan Cox : Uses the improved datagram code.
20 * Alan Cox : Added NULL's for socket options.
21 * Alan Cox : Re-commented the code.
22 * Alan Cox : Use new kernel side addressing
23 * Rob Janssen : Correct MTU usage.
24 * Dave Platt : Counter leaks caused by incorrect
25 * interrupt locking and some slightly
26 * dubious gcc output. Can you read
27 * compiler: it said _VOLATILE_
28 * Richard Kooijman : Timestamp fixes.
29 * Alan Cox : New buffers. Use sk->mac.raw.
30 * Alan Cox : sendmsg/recvmsg support.
31 * Alan Cox : Protocol setting support
32 * Alexey Kuznetsov : Untied from IPv4 stack.
33 * Cyrus Durgin : Fixed kerneld for kmod.
34 * Michal Ostrowski : Module initialization cleanup.
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090035 * Ulises Alonso : Frame number limit removal and
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070037 * Eric Biederman : Allow for > 8 byte hardware addresses.
38 * The convention is that longer addresses
39 * will simply extend the hardware address
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090040 * byte arrays at the end of sockaddr_ll
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070041 * and packet_mreq.
Johann Baudy69e3c752009-05-18 22:11:22 -070042 * Johann Baudy : Added TX RING.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 * This program is free software; you can redistribute it and/or
45 * modify it under the terms of the GNU General Public License
46 * as published by the Free Software Foundation; either version
47 * 2 of the License, or (at your option) any later version.
48 *
49 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080053#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/fcntl.h>
55#include <linux/socket.h>
56#include <linux/in.h>
57#include <linux/inet.h>
58#include <linux/netdevice.h>
59#include <linux/if_packet.h>
60#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080061#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080064#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/ip.h>
67#include <net/protocol.h>
68#include <linux/skbuff.h>
69#include <net/sock.h>
70#include <linux/errno.h>
71#include <linux/timer.h>
72#include <asm/system.h>
73#include <asm/uaccess.h>
74#include <asm/ioctls.h>
75#include <asm/page.h>
Al Viroa1f8e7f2006-10-19 16:08:53 -040076#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <asm/io.h>
78#include <linux/proc_fs.h>
79#include <linux/seq_file.h>
80#include <linux/poll.h>
81#include <linux/module.h>
82#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080083#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070084#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080085#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000086#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070087#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#ifdef CONFIG_INET
90#include <net/inet_common.h>
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 Assumptions:
95 - if device has no dev->hard_header routine, it adds and removes ll header
96 inside itself. In this case ll header is invisible outside of device,
97 but higher levels still should reserve dev->hard_header_len.
98 Some devices are enough clever to reallocate skb, when header
99 will not fit to reserved space (tunnel), another ones are silly
100 (PPP).
101 - packet socket receives packets with pulled ll header,
102 so that SOCK_RAW should push it back.
103
104On receive:
105-----------
106
107Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700108 mac_header -> ll header
109 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700112 mac_header -> ll header
113 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700116 mac_header -> UNKNOWN position. It is very likely, that it points to ll
117 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900118 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700119 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700122 mac_header -> data. ll header is still not built!
123 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125Resume
126 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
127
128
129On transmit:
130------------
131
132dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700133 mac_header -> ll header
134 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700137 mac_header -> data
138 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 We should set nh.raw on output to correct posistion,
141 packet classifier depends on it.
142 */
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Private packet socket structures. */
145
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000146struct packet_mclist {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct packet_mclist *next;
148 int ifindex;
149 int count;
150 unsigned short type;
151 unsigned short alen;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700152 unsigned char addr[MAX_ADDR_LEN];
153};
154/* identical to struct packet_mreq except it has
155 * a longer address field.
156 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000157struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700158 int mr_ifindex;
159 unsigned short mr_type;
160 unsigned short mr_alen;
161 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
David S. Millera2efcfa2007-05-29 13:12:50 -0700163
Johann Baudy69e3c752009-05-18 22:11:22 -0700164static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
165 int closing, int tx_ring);
166
Neil Horman0e3125c2010-11-16 10:26:47 -0800167struct pgv {
168 char *buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -0800169};
170
Johann Baudy69e3c752009-05-18 22:11:22 -0700171struct packet_ring_buffer {
Neil Horman0e3125c2010-11-16 10:26:47 -0800172 struct pgv *pg_vec;
Johann Baudy69e3c752009-05-18 22:11:22 -0700173 unsigned int head;
174 unsigned int frames_per_block;
175 unsigned int frame_size;
176 unsigned int frame_max;
177
178 unsigned int pg_vec_order;
179 unsigned int pg_vec_pages;
180 unsigned int pg_vec_len;
181
182 atomic_t pending;
183};
184
185struct packet_sock;
186static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188static void packet_flush_mclist(struct sock *sk);
189
190struct packet_sock {
191 /* struct sock has to be the first member of packet_sock */
192 struct sock sk;
193 struct tpacket_stats stats;
Johann Baudy69e3c752009-05-18 22:11:22 -0700194 struct packet_ring_buffer rx_ring;
195 struct packet_ring_buffer tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int copy_thresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spinlock_t bind_lock;
Herbert Xu905db442009-01-30 14:12:06 -0800198 struct mutex pg_vec_lock;
Herbert Xu8dc41942007-02-04 23:31:32 -0800199 unsigned int running:1, /* prot_hook is attached*/
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700200 auxdata:1,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -0800201 origdev:1,
202 has_vnet_hdr:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int ifindex; /* bound device */
Al Viro0e11c912006-11-08 00:26:29 -0800204 __be16 num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct packet_mclist *mclist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 atomic_t mapped;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700207 enum tpacket_versions tp_version;
208 unsigned int tp_hdrlen;
Patrick McHardy89133362008-07-18 18:05:19 -0700209 unsigned int tp_reserve;
Johann Baudy69e3c752009-05-18 22:11:22 -0700210 unsigned int tp_loss:1;
Scott McMillan614f60f2010-06-02 05:53:56 -0700211 unsigned int tp_tstamp;
Eric Dumazet94b05952009-10-16 04:02:20 +0000212 struct packet_type prot_hook ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Herbert Xuffbc6112007-02-04 23:33:10 -0800215struct packet_skb_cb {
216 unsigned int origlen;
217 union {
218 struct sockaddr_pkt pkt;
219 struct sockaddr_ll ll;
220 } sa;
221};
222
223#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800224
Changli Gaof6dafa92010-12-07 04:26:16 +0000225static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000226{
227 if (is_vmalloc_addr(addr))
228 return vmalloc_to_page(addr);
229 return virt_to_page(addr);
230}
231
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700232static void __packet_set_status(struct packet_sock *po, void *frame, int status)
233{
234 union {
235 struct tpacket_hdr *h1;
236 struct tpacket2_hdr *h2;
237 void *raw;
238 } h;
239
240 h.raw = frame;
241 switch (po->tp_version) {
242 case TPACKET_V1:
243 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000244 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700245 break;
246 case TPACKET_V2:
247 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000248 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700249 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700250 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000251 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700252 BUG();
253 }
254
255 smp_wmb();
256}
257
258static int __packet_get_status(struct packet_sock *po, void *frame)
259{
260 union {
261 struct tpacket_hdr *h1;
262 struct tpacket2_hdr *h2;
263 void *raw;
264 } h;
265
266 smp_rmb();
267
268 h.raw = frame;
269 switch (po->tp_version) {
270 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000271 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700272 return h.h1->tp_status;
273 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000274 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700275 return h.h2->tp_status;
276 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000277 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700278 BUG();
279 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Johann Baudy69e3c752009-05-18 22:11:22 -0700282
283static void *packet_lookup_frame(struct packet_sock *po,
284 struct packet_ring_buffer *rb,
285 unsigned int position,
286 int status)
287{
288 unsigned int pg_vec_pos, frame_offset;
289 union {
290 struct tpacket_hdr *h1;
291 struct tpacket2_hdr *h2;
292 void *raw;
293 } h;
294
295 pg_vec_pos = position / rb->frames_per_block;
296 frame_offset = position % rb->frames_per_block;
297
Neil Horman0e3125c2010-11-16 10:26:47 -0800298 h.raw = rb->pg_vec[pg_vec_pos].buffer +
299 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700300
301 if (status != __packet_get_status(po, h.raw))
302 return NULL;
303
304 return h.raw;
305}
306
307static inline void *packet_current_frame(struct packet_sock *po,
308 struct packet_ring_buffer *rb,
309 int status)
310{
311 return packet_lookup_frame(po, rb, rb->head, status);
312}
313
314static inline void *packet_previous_frame(struct packet_sock *po,
315 struct packet_ring_buffer *rb,
316 int status)
317{
318 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
319 return packet_lookup_frame(po, rb, previous, status);
320}
321
322static inline void packet_increment_head(struct packet_ring_buffer *buff)
323{
324 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static inline struct packet_sock *pkt_sk(struct sock *sk)
328{
329 return (struct packet_sock *)sk;
330}
331
332static void packet_sock_destruct(struct sock *sk)
333{
Richard Cochraned85b562010-04-07 22:41:28 +0000334 skb_queue_purge(&sk->sk_error_queue);
335
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700336 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
337 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000340 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return;
342 }
343
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800344 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800348static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800350static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000352static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
353 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sock *sk;
356 struct sockaddr_pkt *spkt;
357
358 /*
359 * When we registered the protocol we saved the socket in the data
360 * field for just this event.
361 */
362
363 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * Yank back the headers [hope the device set this
367 * right or kerboom...]
368 *
369 * Incoming packets have ll header pulled,
370 * push it back.
371 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700372 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * so that this procedure is noop.
374 */
375
376 if (skb->pkt_type == PACKET_LOOPBACK)
377 goto out;
378
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800379 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800380 goto out;
381
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000382 skb = skb_share_check(skb, GFP_ATOMIC);
383 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto oom;
385
386 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000387 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Phil Oester84531c22005-07-12 11:57:52 -0700389 /* drop conntrack reference */
390 nf_reset(skb);
391
Herbert Xuffbc6112007-02-04 23:33:10 -0800392 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700394 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * The SOCK_PACKET socket receives _all_ frames.
398 */
399
400 spkt->spkt_family = dev->type;
401 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
402 spkt->spkt_protocol = skb->protocol;
403
404 /*
405 * Charge the memory to the socket. This is done specifically
406 * to prevent sockets using all the memory up.
407 */
408
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000409 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411
412out:
413 kfree_skb(skb);
414oom:
415 return 0;
416}
417
418
419/*
420 * Output a raw packet to a device layer. This bypasses all the other
421 * protocol layers and you must therefore supply it with a complete frame
422 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
425 struct msghdr *msg, size_t len)
426{
427 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000428 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000429 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000431 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900435 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000438 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000440 return -EINVAL;
441 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
442 proto = saddr->spkt_protocol;
443 } else
444 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900447 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
449
450 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000451retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100452 rcu_read_lock();
453 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 err = -ENODEV;
455 if (dev == NULL)
456 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900457
David S. Millerd5e76b02007-01-25 19:30:36 -0800458 err = -ENETDOWN;
459 if (!(dev->flags & IFF_UP))
460 goto out_unlock;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000463 * You may not queue a frame bigger than the mtu. This is the lowest level
464 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 err = -EMSGSIZE;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -0800468 if (len > dev->mtu + dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto out_unlock;
470
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000471 if (!skb) {
472 size_t reserved = LL_RESERVED_SPACE(dev);
473 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000475 rcu_read_unlock();
476 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
477 if (skb == NULL)
478 return -ENOBUFS;
479 /* FIXME: Save some space for broken drivers that write a hard
480 * header at transmission time by themselves. PPP is the notable
481 * one here. This should really be fixed at the driver level.
482 */
483 skb_reserve(skb, reserved);
484 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900485
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000486 /* Try to align data part correctly */
487 if (hhlen) {
488 skb->data -= hhlen;
489 skb->tail -= hhlen;
490 if (len < hhlen)
491 skb_reset_network_header(skb);
492 }
493 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
494 if (err)
495 goto out_free;
496 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 skb->protocol = proto;
501 skb->dev = dev;
502 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000503 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000504 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000505 if (err < 0)
506 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100509 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000510 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100513 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000514out_free:
515 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return err;
517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Eric Dumazet62ab0812010-12-06 20:50:09 +0000519static inline unsigned int run_filter(const struct sk_buff *skb,
520 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800521 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct sk_filter *filter;
524
Eric Dumazet80f8f102011-01-18 07:46:52 +0000525 rcu_read_lock();
526 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800527 if (filter != NULL)
Eric Dumazet93aaae22010-11-19 09:49:59 -0800528 res = sk_run_filter(skb, filter->insns);
Eric Dumazet80f8f102011-01-18 07:46:52 +0000529 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
David S. Millerdbcb5852007-01-24 15:21:02 -0800531 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000535 * This function makes lazy skb cloning in hope that most of packets
536 * are discarded by BPF.
537 *
538 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
539 * and skb->cb are mangled. It works because (and until) packets
540 * falling here are owned by current CPU. Output packets are cloned
541 * by dev_queue_xmit_nit(), input packets are processed by net_bh
542 * sequencially, so that if we return skb to original state on exit,
543 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000546static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
547 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct sock *sk;
550 struct sockaddr_ll *sll;
551 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000552 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800554 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (skb->pkt_type == PACKET_LOOPBACK)
557 goto drop;
558
559 sk = pt->af_packet_priv;
560 po = pkt_sk(sk);
561
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800562 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800563 goto drop;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 skb->dev = dev;
566
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700567 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000569 * exported to higher levels.
570 *
571 * Otherwise, the device hides details of its frame
572 * structure, so that corresponding packet head is
573 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700576 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 else if (skb->pkt_type == PACKET_OUTGOING) {
578 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300579 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
582
583 snaplen = skb->len;
584
David S. Millerdbcb5852007-01-24 15:21:02 -0800585 res = run_filter(skb, sk, snaplen);
586 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700587 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800588 if (snaplen > res)
589 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
592 (unsigned)sk->sk_rcvbuf)
593 goto drop_n_acct;
594
595 if (skb_shared(skb)) {
596 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
597 if (nskb == NULL)
598 goto drop_n_acct;
599
600 if (skb_head != skb->data) {
601 skb->data = skb_head;
602 skb->len = skb_len;
603 }
604 kfree_skb(skb);
605 skb = nskb;
606 }
607
Herbert Xuffbc6112007-02-04 23:33:10 -0800608 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
609 sizeof(skb->cb));
610
611 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sll->sll_family = AF_PACKET;
613 sll->sll_hatype = dev->type;
614 sll->sll_protocol = skb->protocol;
615 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800616 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700617 sll->sll_ifindex = orig_dev->ifindex;
618 else
619 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700621 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Herbert Xuffbc6112007-02-04 23:33:10 -0800623 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (pskb_trim(skb, snaplen))
626 goto drop_n_acct;
627
628 skb_set_owner_r(skb, sk);
629 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000630 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Phil Oester84531c22005-07-12 11:57:52 -0700632 /* drop conntrack reference */
633 nf_reset(skb);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 spin_lock(&sk->sk_receive_queue.lock);
636 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700637 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 __skb_queue_tail(&sk->sk_receive_queue, skb);
639 spin_unlock(&sk->sk_receive_queue.lock);
640 sk->sk_data_ready(sk, skb->len);
641 return 0;
642
643drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700644 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646drop_n_restore:
647 if (skb_head != skb->data && skb_shared(skb)) {
648 skb->data = skb_head;
649 skb->len = skb_len;
650 }
651drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000652 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
655
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000656static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
657 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct sock *sk;
660 struct packet_sock *po;
661 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700662 union {
663 struct tpacket_hdr *h1;
664 struct tpacket2_hdr *h2;
665 void *raw;
666 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000667 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800669 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700671 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700673 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700674 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -0700675 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (skb->pkt_type == PACKET_LOOPBACK)
678 goto drop;
679
680 sk = pt->af_packet_priv;
681 po = pkt_sk(sk);
682
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800683 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800684 goto drop;
685
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700686 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700688 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 else if (skb->pkt_type == PACKET_OUTGOING) {
690 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300691 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 }
694
Herbert Xu8dc41942007-02-04 23:31:32 -0800695 if (skb->ip_summed == CHECKSUM_PARTIAL)
696 status |= TP_STATUS_CSUMNOTREADY;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 snaplen = skb->len;
699
David S. Millerdbcb5852007-01-24 15:21:02 -0800700 res = run_filter(skb, sk, snaplen);
701 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700702 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800703 if (snaplen > res)
704 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy89133362008-07-18 18:05:19 -0700707 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
708 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300710 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700711 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy89133362008-07-18 18:05:19 -0700712 (maclen < 16 ? 16 : maclen)) +
713 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 macoff = netoff - maclen;
715 }
716
Johann Baudy69e3c752009-05-18 22:11:22 -0700717 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (po->copy_thresh &&
719 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
720 (unsigned)sk->sk_rcvbuf) {
721 if (skb_shared(skb)) {
722 copy_skb = skb_clone(skb, GFP_ATOMIC);
723 } else {
724 copy_skb = skb_get(skb);
725 skb_head = skb->data;
726 }
727 if (copy_skb)
728 skb_set_owner_r(copy_skb, sk);
729 }
Johann Baudy69e3c752009-05-18 22:11:22 -0700730 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if ((int)snaplen < 0)
732 snaplen = 0;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -0700736 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700737 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -0700739 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 po->stats.tp_packets++;
741 if (copy_skb) {
742 status |= TP_STATUS_COPY;
743 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
744 }
745 if (!po->stats.tp_drops)
746 status &= ~TP_STATUS_LOSING;
747 spin_unlock(&sk->sk_receive_queue.lock);
748
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700749 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700751 switch (po->tp_version) {
752 case TPACKET_V1:
753 h.h1->tp_len = skb->len;
754 h.h1->tp_snaplen = snaplen;
755 h.h1->tp_mac = macoff;
756 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700757 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
758 && shhwtstamps->syststamp.tv64)
759 tv = ktime_to_timeval(shhwtstamps->syststamp);
760 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
761 && shhwtstamps->hwtstamp.tv64)
762 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
763 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700764 tv = ktime_to_timeval(skb->tstamp);
765 else
766 do_gettimeofday(&tv);
767 h.h1->tp_sec = tv.tv_sec;
768 h.h1->tp_usec = tv.tv_usec;
769 hdrlen = sizeof(*h.h1);
770 break;
771 case TPACKET_V2:
772 h.h2->tp_len = skb->len;
773 h.h2->tp_snaplen = snaplen;
774 h.h2->tp_mac = macoff;
775 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700776 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
777 && shhwtstamps->syststamp.tv64)
778 ts = ktime_to_timespec(shhwtstamps->syststamp);
779 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
780 && shhwtstamps->hwtstamp.tv64)
781 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
782 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700783 ts = ktime_to_timespec(skb->tstamp);
784 else
785 getnstimeofday(&ts);
786 h.h2->tp_sec = ts.tv_sec;
787 h.h2->tp_nsec = ts.tv_nsec;
Eric Dumazet05423b22009-10-26 18:40:35 -0700788 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700789 hdrlen = sizeof(*h.h2);
790 break;
791 default:
792 BUG();
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700795 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700796 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 sll->sll_family = AF_PACKET;
798 sll->sll_hatype = dev->type;
799 sll->sll_protocol = skb->protocol;
800 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800801 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700802 sll->sll_ifindex = orig_dev->ifindex;
803 else
804 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700806 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -0800807 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +0000808#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 {
Changli Gao0af55bb2010-12-01 02:52:20 +0000810 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Changli Gao0af55bb2010-12-01 02:52:20 +0000812 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
813 for (start = h.raw; start < end; start += PAGE_SIZE)
814 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Changli Gaof6dafa92010-12-07 04:26:16 +0000816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 sk->sk_data_ready(sk, 0);
819
820drop_n_restore:
821 if (skb_head != skb->data && skb_shared(skb)) {
822 skb->data = skb_head;
823 skb->len = skb_len;
824 }
825drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900826 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828
829ring_is_full:
830 po->stats.tp_drops++;
831 spin_unlock(&sk->sk_receive_queue.lock);
832
833 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +0000834 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto drop_n_restore;
836}
837
Johann Baudy69e3c752009-05-18 22:11:22 -0700838static void tpacket_destruct_skb(struct sk_buff *skb)
839{
840 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000841 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -0700842
843 BUG_ON(skb == NULL);
844
845 if (likely(po->tx_ring.pg_vec)) {
846 ph = skb_shinfo(skb)->destructor_arg;
847 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
848 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
849 atomic_dec(&po->tx_ring.pending);
850 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
851 }
852
853 sock_wfree(skb);
854}
855
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000856static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
857 void *frame, struct net_device *dev, int size_max,
858 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -0700859{
860 union {
861 struct tpacket_hdr *h1;
862 struct tpacket2_hdr *h2;
863 void *raw;
864 } ph;
865 int to_write, offset, len, tp_len, nr_frags, len_max;
866 struct socket *sock = po->sk.sk_socket;
867 struct page *page;
868 void *data;
869 int err;
870
871 ph.raw = frame;
872
873 skb->protocol = proto;
874 skb->dev = dev;
875 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000876 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -0700877 skb_shinfo(skb)->destructor_arg = ph.raw;
878
879 switch (po->tp_version) {
880 case TPACKET_V2:
881 tp_len = ph.h2->tp_len;
882 break;
883 default:
884 tp_len = ph.h1->tp_len;
885 break;
886 }
887 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000888 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -0700889 return -EMSGSIZE;
890 }
891
892 skb_reserve(skb, LL_RESERVED_SPACE(dev));
893 skb_reset_network_header(skb);
894
895 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
896 to_write = tp_len;
897
898 if (sock->type == SOCK_DGRAM) {
899 err = dev_hard_header(skb, dev, ntohs(proto), addr,
900 NULL, tp_len);
901 if (unlikely(err < 0))
902 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000903 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -0700904 /* net device doesn't like empty head */
905 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000906 pr_err("packet size is too short (%d < %d)\n",
907 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700908 return -EINVAL;
909 }
910
911 skb_push(skb, dev->hard_header_len);
912 err = skb_store_bits(skb, 0, data,
913 dev->hard_header_len);
914 if (unlikely(err))
915 return err;
916
917 data += dev->hard_header_len;
918 to_write -= dev->hard_header_len;
919 }
920
921 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -0700922 offset = offset_in_page(data);
923 len_max = PAGE_SIZE - offset;
924 len = ((to_write > len_max) ? len_max : to_write);
925
926 skb->data_len = to_write;
927 skb->len += to_write;
928 skb->truesize += to_write;
929 atomic_add(to_write, &po->sk.sk_wmem_alloc);
930
931 while (likely(to_write)) {
932 nr_frags = skb_shinfo(skb)->nr_frags;
933
934 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000935 pr_err("Packet exceed the number of skb frags(%lu)\n",
936 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -0700937 return -EFAULT;
938 }
939
Changli Gao0af55bb2010-12-01 02:52:20 +0000940 page = pgv_to_page(data);
941 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -0700942 flush_dcache_page(page);
943 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +0000944 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700945 to_write -= len;
946 offset = 0;
947 len_max = PAGE_SIZE;
948 len = ((to_write > len_max) ? len_max : to_write);
949 }
950
951 return tp_len;
952}
953
954static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
955{
956 struct socket *sock;
957 struct sk_buff *skb;
958 struct net_device *dev;
959 __be16 proto;
960 int ifindex, err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000961 void *ph;
962 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -0700963 int tp_len, size_max;
964 unsigned char *addr;
965 int len_sum = 0;
966 int status = 0;
967
968 sock = po->sk.sk_socket;
969
970 mutex_lock(&po->pg_vec_lock);
971
972 err = -EBUSY;
973 if (saddr == NULL) {
974 ifindex = po->ifindex;
975 proto = po->num;
976 addr = NULL;
977 } else {
978 err = -EINVAL;
979 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
980 goto out;
981 if (msg->msg_namelen < (saddr->sll_halen
982 + offsetof(struct sockaddr_ll,
983 sll_addr)))
984 goto out;
985 ifindex = saddr->sll_ifindex;
986 proto = saddr->sll_protocol;
987 addr = saddr->sll_addr;
988 }
989
990 dev = dev_get_by_index(sock_net(&po->sk), ifindex);
991 err = -ENXIO;
992 if (unlikely(dev == NULL))
993 goto out;
994
995 reserve = dev->hard_header_len;
996
997 err = -ENETDOWN;
998 if (unlikely(!(dev->flags & IFF_UP)))
999 goto out_put;
1000
1001 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001002 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001003
1004 if (size_max > dev->mtu + reserve)
1005 size_max = dev->mtu + reserve;
1006
1007 do {
1008 ph = packet_current_frame(po, &po->tx_ring,
1009 TP_STATUS_SEND_REQUEST);
1010
1011 if (unlikely(ph == NULL)) {
1012 schedule();
1013 continue;
1014 }
1015
1016 status = TP_STATUS_SEND_REQUEST;
1017 skb = sock_alloc_send_skb(&po->sk,
1018 LL_ALLOCATED_SPACE(dev)
1019 + sizeof(struct sockaddr_ll),
1020 0, &err);
1021
1022 if (unlikely(skb == NULL))
1023 goto out_status;
1024
1025 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1026 addr);
1027
1028 if (unlikely(tp_len < 0)) {
1029 if (po->tp_loss) {
1030 __packet_set_status(po, ph,
1031 TP_STATUS_AVAILABLE);
1032 packet_increment_head(&po->tx_ring);
1033 kfree_skb(skb);
1034 continue;
1035 } else {
1036 status = TP_STATUS_WRONG_FORMAT;
1037 err = tp_len;
1038 goto out_status;
1039 }
1040 }
1041
1042 skb->destructor = tpacket_destruct_skb;
1043 __packet_set_status(po, ph, TP_STATUS_SENDING);
1044 atomic_inc(&po->tx_ring.pending);
1045
1046 status = TP_STATUS_SEND_REQUEST;
1047 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001048 if (unlikely(err > 0)) {
1049 err = net_xmit_errno(err);
1050 if (err && __packet_get_status(po, ph) ==
1051 TP_STATUS_AVAILABLE) {
1052 /* skb was destructed already */
1053 skb = NULL;
1054 goto out_status;
1055 }
1056 /*
1057 * skb was dropped but not destructed yet;
1058 * let's treat it like congestion or err < 0
1059 */
1060 err = 0;
1061 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001062 packet_increment_head(&po->tx_ring);
1063 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001064 } while (likely((ph != NULL) ||
1065 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1066 (atomic_read(&po->tx_ring.pending))))
1067 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001068
1069 err = len_sum;
1070 goto out_put;
1071
Johann Baudy69e3c752009-05-18 22:11:22 -07001072out_status:
1073 __packet_set_status(po, ph, status);
1074 kfree_skb(skb);
1075out_put:
1076 dev_put(dev);
1077out:
1078 mutex_unlock(&po->pg_vec_lock);
1079 return err;
1080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001082static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1083 size_t reserve, size_t len,
1084 size_t linear, int noblock,
1085 int *err)
1086{
1087 struct sk_buff *skb;
1088
1089 /* Under a page? Don't bother with paged skb. */
1090 if (prepad + len < PAGE_SIZE || !linear)
1091 linear = len;
1092
1093 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1094 err);
1095 if (!skb)
1096 return NULL;
1097
1098 skb_reserve(skb, reserve);
1099 skb_put(skb, linear);
1100 skb->data_len = len - linear;
1101 skb->len += len - linear;
1102
1103 return skb;
1104}
1105
Johann Baudy69e3c752009-05-18 22:11:22 -07001106static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct msghdr *msg, size_t len)
1108{
1109 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001110 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct sk_buff *skb;
1112 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001113 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 unsigned char *addr;
1115 int ifindex, err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001116 struct virtio_net_hdr vnet_hdr = { 0 };
1117 int offset = 0;
1118 int vnet_hdr_len;
1119 struct packet_sock *po = pkt_sk(sk);
1120 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001123 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (saddr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ifindex = po->ifindex;
1128 proto = po->num;
1129 addr = NULL;
1130 } else {
1131 err = -EINVAL;
1132 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1133 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001134 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1135 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 ifindex = saddr->sll_ifindex;
1137 proto = saddr->sll_protocol;
1138 addr = saddr->sll_addr;
1139 }
1140
1141
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001142 dev = dev_get_by_index(sock_net(sk), ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 err = -ENXIO;
1144 if (dev == NULL)
1145 goto out_unlock;
1146 if (sock->type == SOCK_RAW)
1147 reserve = dev->hard_header_len;
1148
David S. Millerd5e76b02007-01-25 19:30:36 -08001149 err = -ENETDOWN;
1150 if (!(dev->flags & IFF_UP))
1151 goto out_unlock;
1152
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001153 if (po->has_vnet_hdr) {
1154 vnet_hdr_len = sizeof(vnet_hdr);
1155
1156 err = -EINVAL;
1157 if (len < vnet_hdr_len)
1158 goto out_unlock;
1159
1160 len -= vnet_hdr_len;
1161
1162 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1163 vnet_hdr_len);
1164 if (err < 0)
1165 goto out_unlock;
1166
1167 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1168 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1169 vnet_hdr.hdr_len))
1170 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1171 vnet_hdr.csum_offset + 2;
1172
1173 err = -EINVAL;
1174 if (vnet_hdr.hdr_len > len)
1175 goto out_unlock;
1176
1177 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1178 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1179 case VIRTIO_NET_HDR_GSO_TCPV4:
1180 gso_type = SKB_GSO_TCPV4;
1181 break;
1182 case VIRTIO_NET_HDR_GSO_TCPV6:
1183 gso_type = SKB_GSO_TCPV6;
1184 break;
1185 case VIRTIO_NET_HDR_GSO_UDP:
1186 gso_type = SKB_GSO_UDP;
1187 break;
1188 default:
1189 goto out_unlock;
1190 }
1191
1192 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1193 gso_type |= SKB_GSO_TCP_ECN;
1194
1195 if (vnet_hdr.gso_size == 0)
1196 goto out_unlock;
1197
1198 }
1199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 err = -EMSGSIZE;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001202 if (!gso_type && (len > dev->mtu+reserve))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto out_unlock;
1204
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001205 err = -ENOBUFS;
1206 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1207 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1208 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001209 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto out_unlock;
1211
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001212 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001214 err = -EINVAL;
1215 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001216 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001217 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001220 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (err)
1222 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001223 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001224 if (err < 0)
1225 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 skb->protocol = proto;
1228 skb->dev = dev;
1229 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001230 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001232 if (po->has_vnet_hdr) {
1233 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1234 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1235 vnet_hdr.csum_offset)) {
1236 err = -EINVAL;
1237 goto out_free;
1238 }
1239 }
1240
1241 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1242 skb_shinfo(skb)->gso_type = gso_type;
1243
1244 /* Header must be checked, and gso_segs computed. */
1245 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1246 skb_shinfo(skb)->gso_segs = 0;
1247
1248 len += vnet_hdr_len;
1249 }
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Now send it
1253 */
1254
1255 err = dev_queue_xmit(skb);
1256 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1257 goto out_unlock;
1258
1259 dev_put(dev);
1260
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001261 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263out_free:
1264 kfree_skb(skb);
1265out_unlock:
1266 if (dev)
1267 dev_put(dev);
1268out:
1269 return err;
1270}
1271
Johann Baudy69e3c752009-05-18 22:11:22 -07001272static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1273 struct msghdr *msg, size_t len)
1274{
Johann Baudy69e3c752009-05-18 22:11:22 -07001275 struct sock *sk = sock->sk;
1276 struct packet_sock *po = pkt_sk(sk);
1277 if (po->tx_ring.pg_vec)
1278 return tpacket_snd(po, msg);
1279 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001280 return packet_snd(sock, msg, len);
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/*
1284 * Close a PACKET socket. This is fairly simple. We immediately go
1285 * to 'closed' state and remove our protocol entry in the device list.
1286 */
1287
1288static int packet_release(struct socket *sock)
1289{
1290 struct sock *sk = sock->sk;
1291 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001292 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001293 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (!sk)
1296 return 0;
1297
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001298 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 po = pkt_sk(sk);
1300
stephen hemminger808f5112010-02-22 07:57:18 +00001301 spin_lock_bh(&net->packet.sklist_lock);
1302 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001303 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001304 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
stephen hemminger808f5112010-02-22 07:57:18 +00001306 spin_lock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (po->running) {
1308 /*
stephen hemminger808f5112010-02-22 07:57:18 +00001309 * Remove from protocol table
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 po->running = 0;
1312 po->num = 0;
stephen hemminger808f5112010-02-22 07:57:18 +00001313 __dev_remove_pack(&po->prot_hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 __sock_put(sk);
1315 }
stephen hemminger808f5112010-02-22 07:57:18 +00001316 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Johann Baudy69e3c752009-05-18 22:11:22 -07001320 memset(&req, 0, sizeof(req));
1321
1322 if (po->rx_ring.pg_vec)
1323 packet_set_ring(sk, &req, 1, 0);
1324
1325 if (po->tx_ring.pg_vec)
1326 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
stephen hemminger808f5112010-02-22 07:57:18 +00001328 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * Now the socket is dead. No more input will appear.
1331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 sock_orphan(sk);
1333 sock->sk = NULL;
1334
1335 /* Purge queues */
1336
1337 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001338 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 sock_put(sk);
1341 return 0;
1342}
1343
1344/*
1345 * Attach a packet hook.
1346 */
1347
Al Viro0e11c912006-11-08 00:26:29 -08001348static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct packet_sock *po = pkt_sk(sk);
1351 /*
1352 * Detach an existing hook if present.
1353 */
1354
1355 lock_sock(sk);
1356
1357 spin_lock(&po->bind_lock);
1358 if (po->running) {
1359 __sock_put(sk);
1360 po->running = 0;
1361 po->num = 0;
1362 spin_unlock(&po->bind_lock);
1363 dev_remove_pack(&po->prot_hook);
1364 spin_lock(&po->bind_lock);
1365 }
1366
1367 po->num = protocol;
1368 po->prot_hook.type = protocol;
1369 po->prot_hook.dev = dev;
1370
1371 po->ifindex = dev ? dev->ifindex : 0;
1372
1373 if (protocol == 0)
1374 goto out_unlock;
1375
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001376 if (!dev || (dev->flags & IFF_UP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 dev_add_pack(&po->prot_hook);
1378 sock_hold(sk);
1379 po->running = 1;
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001380 } else {
1381 sk->sk_err = ENETDOWN;
1382 if (!sock_flag(sk, SOCK_DEAD))
1383 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
1386out_unlock:
1387 spin_unlock(&po->bind_lock);
1388 release_sock(sk);
1389 return 0;
1390}
1391
1392/*
1393 * Bind a packet socket to a device
1394 */
1395
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001396static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1397 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001399 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 char name[15];
1401 struct net_device *dev;
1402 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /*
1405 * Check legality
1406 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001407
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001408 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001410 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001412 dev = dev_get_by_name(sock_net(sk), name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (dev) {
1414 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
1415 dev_put(dev);
1416 }
1417 return err;
1418}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1421{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001422 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1423 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct net_device *dev = NULL;
1425 int err;
1426
1427
1428 /*
1429 * Check legality
1430 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (addr_len < sizeof(struct sockaddr_ll))
1433 return -EINVAL;
1434 if (sll->sll_family != AF_PACKET)
1435 return -EINVAL;
1436
1437 if (sll->sll_ifindex) {
1438 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001439 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (dev == NULL)
1441 goto out;
1442 }
1443 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
1444 if (dev)
1445 dev_put(dev);
1446
1447out:
1448 return err;
1449}
1450
1451static struct proto packet_proto = {
1452 .name = "PACKET",
1453 .owner = THIS_MODULE,
1454 .obj_size = sizeof(struct packet_sock),
1455};
1456
1457/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001458 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
1460
Eric Paris3f378b62009-11-05 22:18:14 -08001461static int packet_create(struct net *net, struct socket *sock, int protocol,
1462 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct sock *sk;
1465 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001466 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int err;
1468
1469 if (!capable(CAP_NET_RAW))
1470 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001471 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1472 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return -ESOCKTNOSUPPORT;
1474
1475 sock->state = SS_UNCONNECTED;
1476
1477 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001478 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (sk == NULL)
1480 goto out;
1481
1482 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (sock->type == SOCK_PACKET)
1484 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 sock_init_data(sock, sk);
1487
1488 po = pkt_sk(sk);
1489 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001490 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001493 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /*
1496 * Attach a protocol block
1497 */
1498
1499 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001500 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (sock->type == SOCK_PACKET)
1504 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 po->prot_hook.af_packet_priv = sk;
1507
Al Viro0e11c912006-11-08 00:26:29 -08001508 if (proto) {
1509 po->prot_hook.type = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 dev_add_pack(&po->prot_hook);
1511 sock_hold(sk);
1512 po->running = 1;
1513 }
1514
stephen hemminger808f5112010-02-22 07:57:18 +00001515 spin_lock_bh(&net->packet.sklist_lock);
1516 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001517 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001518 spin_unlock_bh(&net->packet.sklist_lock);
1519
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521out:
1522 return err;
1523}
1524
Richard Cochraned85b562010-04-07 22:41:28 +00001525static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1526{
1527 struct sock_exterr_skb *serr;
1528 struct sk_buff *skb, *skb2;
1529 int copied, err;
1530
1531 err = -EAGAIN;
1532 skb = skb_dequeue(&sk->sk_error_queue);
1533 if (skb == NULL)
1534 goto out;
1535
1536 copied = skb->len;
1537 if (copied > len) {
1538 msg->msg_flags |= MSG_TRUNC;
1539 copied = len;
1540 }
1541 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1542 if (err)
1543 goto out_free_skb;
1544
1545 sock_recv_timestamp(msg, sk, skb);
1546
1547 serr = SKB_EXT_ERR(skb);
1548 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1549 sizeof(serr->ee), &serr->ee);
1550
1551 msg->msg_flags |= MSG_ERRQUEUE;
1552 err = copied;
1553
1554 /* Reset and regenerate socket error */
1555 spin_lock_bh(&sk->sk_error_queue.lock);
1556 sk->sk_err = 0;
1557 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1558 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1559 spin_unlock_bh(&sk->sk_error_queue.lock);
1560 sk->sk_error_report(sk);
1561 } else
1562 spin_unlock_bh(&sk->sk_error_queue.lock);
1563
1564out_free_skb:
1565 kfree_skb(skb);
1566out:
1567 return err;
1568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570/*
1571 * Pull a packet from our receive queue and hand it to the user.
1572 * If necessary we block.
1573 */
1574
1575static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1576 struct msghdr *msg, size_t len, int flags)
1577{
1578 struct sock *sk = sock->sk;
1579 struct sk_buff *skb;
1580 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001581 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001582 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001585 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 goto out;
1587
1588#if 0
1589 /* What error should we return now? EUNATTACH? */
1590 if (pkt_sk(sk)->ifindex < 0)
1591 return -ENODEV;
1592#endif
1593
Richard Cochraned85b562010-04-07 22:41:28 +00001594 if (flags & MSG_ERRQUEUE) {
1595 err = packet_recv_error(sk, msg, len);
1596 goto out;
1597 }
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 * Call the generic datagram receiver. This handles all sorts
1601 * of horrible races and re-entrancy so we can forget about it
1602 * in the protocol layers.
1603 *
1604 * Now it will return ENETDOWN, if device have just gone down,
1605 * but then it will block.
1606 */
1607
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001608 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001611 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * handles the blocking we don't see and worry about blocking
1613 * retries.
1614 */
1615
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001616 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto out;
1618
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001619 if (pkt_sk(sk)->has_vnet_hdr) {
1620 struct virtio_net_hdr vnet_hdr = { 0 };
1621
1622 err = -EINVAL;
1623 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001624 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001625 goto out_free;
1626
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001627 len -= vnet_hdr_len;
1628
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001629 if (skb_is_gso(skb)) {
1630 struct skb_shared_info *sinfo = skb_shinfo(skb);
1631
1632 /* This is a hint as to how much should be linear. */
1633 vnet_hdr.hdr_len = skb_headlen(skb);
1634 vnet_hdr.gso_size = sinfo->gso_size;
1635 if (sinfo->gso_type & SKB_GSO_TCPV4)
1636 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1637 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1638 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1639 else if (sinfo->gso_type & SKB_GSO_UDP)
1640 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1641 else if (sinfo->gso_type & SKB_GSO_FCOE)
1642 goto out_free;
1643 else
1644 BUG();
1645 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1646 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1647 } else
1648 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1649
1650 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1651 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001652 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001653 vnet_hdr.csum_offset = skb->csum_offset;
1654 } /* else everything is zero */
1655
1656 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1657 vnet_hdr_len);
1658 if (err < 0)
1659 goto out_free;
1660 }
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001663 * If the address length field is there to be filled in, we fill
1664 * it in now.
1665 */
1666
Herbert Xuffbc6112007-02-04 23:33:10 -08001667 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001668 if (sock->type == SOCK_PACKET)
1669 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1670 else
1671 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1672
1673 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 * You lose any data beyond the buffer you gave. If it worries a
1675 * user program they can ask the device for its MTU anyway.
1676 */
1677
1678 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001679 if (copied > len) {
1680 copied = len;
1681 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
1684 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1685 if (err)
1686 goto out_free;
1687
Neil Horman3b885782009-10-12 13:26:31 -07001688 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08001691 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1692 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Herbert Xu8dc41942007-02-04 23:31:32 -08001694 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08001695 struct tpacket_auxdata aux;
1696
1697 aux.tp_status = TP_STATUS_USER;
1698 if (skb->ip_summed == CHECKSUM_PARTIAL)
1699 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1700 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1701 aux.tp_snaplen = skb->len;
1702 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001703 aux.tp_net = skb_network_offset(skb);
Eric Dumazet05423b22009-10-26 18:40:35 -07001704 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
Herbert Xuffbc6112007-02-04 23:33:10 -08001705
1706 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08001707 }
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /*
1710 * Free or return the buffer as appropriate. Again this
1711 * hides all the races and re-entrancy issues from us.
1712 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001713 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715out_free:
1716 skb_free_datagram(sk, skb);
1717out:
1718 return err;
1719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1722 int *uaddr_len, int peer)
1723{
1724 struct net_device *dev;
1725 struct sock *sk = sock->sk;
1726
1727 if (peer)
1728 return -EOPNOTSUPP;
1729
1730 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001731 rcu_read_lock();
1732 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
1733 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001734 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001735 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001737 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 *uaddr_len = sizeof(*uaddr);
1739
1740 return 0;
1741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1744 int *uaddr_len, int peer)
1745{
1746 struct net_device *dev;
1747 struct sock *sk = sock->sk;
1748 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001749 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (peer)
1752 return -EOPNOTSUPP;
1753
1754 sll->sll_family = AF_PACKET;
1755 sll->sll_ifindex = po->ifindex;
1756 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001757 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001758 rcu_read_lock();
1759 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (dev) {
1761 sll->sll_hatype = dev->type;
1762 sll->sll_halen = dev->addr_len;
1763 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 } else {
1765 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1766 sll->sll_halen = 0;
1767 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01001768 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001769 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 return 0;
1772}
1773
Wang Chen2aeb0b82008-07-14 20:49:46 -07001774static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
1775 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 switch (i->type) {
1778 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001779 if (i->alen != dev->addr_len)
1780 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (what > 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001782 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 else
Jiri Pirko22bedad2010-04-01 21:22:57 +00001784 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 break;
1786 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001787 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 break;
1789 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001790 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001792 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001793 if (i->alen != dev->addr_len)
1794 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001795 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001796 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001797 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001798 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001799 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001800 default:
1801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07001803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1807{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001808 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (i->ifindex == dev->ifindex)
1810 packet_dev_mc(dev, i, what);
1811 }
1812}
1813
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001814static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
1816 struct packet_sock *po = pkt_sk(sk);
1817 struct packet_mclist *ml, *i;
1818 struct net_device *dev;
1819 int err;
1820
1821 rtnl_lock();
1822
1823 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001824 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (!dev)
1826 goto done;
1827
1828 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00001829 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto done;
1831
1832 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001833 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (i == NULL)
1835 goto done;
1836
1837 err = 0;
1838 for (ml = po->mclist; ml; ml = ml->next) {
1839 if (ml->ifindex == mreq->mr_ifindex &&
1840 ml->type == mreq->mr_type &&
1841 ml->alen == mreq->mr_alen &&
1842 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1843 ml->count++;
1844 /* Free the new element ... */
1845 kfree(i);
1846 goto done;
1847 }
1848 }
1849
1850 i->type = mreq->mr_type;
1851 i->ifindex = mreq->mr_ifindex;
1852 i->alen = mreq->mr_alen;
1853 memcpy(i->addr, mreq->mr_address, i->alen);
1854 i->count = 1;
1855 i->next = po->mclist;
1856 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07001857 err = packet_dev_mc(dev, i, 1);
1858 if (err) {
1859 po->mclist = i->next;
1860 kfree(i);
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863done:
1864 rtnl_unlock();
1865 return err;
1866}
1867
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001868static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
1870 struct packet_mclist *ml, **mlp;
1871
1872 rtnl_lock();
1873
1874 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1875 if (ml->ifindex == mreq->mr_ifindex &&
1876 ml->type == mreq->mr_type &&
1877 ml->alen == mreq->mr_alen &&
1878 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1879 if (--ml->count == 0) {
1880 struct net_device *dev;
1881 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001882 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1883 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 kfree(ml);
1886 }
1887 rtnl_unlock();
1888 return 0;
1889 }
1890 }
1891 rtnl_unlock();
1892 return -EADDRNOTAVAIL;
1893}
1894
1895static void packet_flush_mclist(struct sock *sk)
1896{
1897 struct packet_sock *po = pkt_sk(sk);
1898 struct packet_mclist *ml;
1899
1900 if (!po->mclist)
1901 return;
1902
1903 rtnl_lock();
1904 while ((ml = po->mclist) != NULL) {
1905 struct net_device *dev;
1906
1907 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001908 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1909 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 kfree(ml);
1912 }
1913 rtnl_unlock();
1914}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916static int
David S. Millerb7058842009-09-30 16:12:20 -07001917packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08001920 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 int ret;
1922
1923 if (level != SOL_PACKET)
1924 return -ENOPROTOOPT;
1925
Johann Baudy69e3c752009-05-18 22:11:22 -07001926 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001927 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 case PACKET_DROP_MEMBERSHIP:
1929 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001930 struct packet_mreq_max mreq;
1931 int len = optlen;
1932 memset(&mreq, 0, sizeof(mreq));
1933 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001935 if (len > sizeof(mreq))
1936 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001937 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001939 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (optname == PACKET_ADD_MEMBERSHIP)
1942 ret = packet_mc_add(sk, &mreq);
1943 else
1944 ret = packet_mc_drop(sk, &mreq);
1945 return ret;
1946 }
David S. Millera2efcfa2007-05-29 13:12:50 -07001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07001949 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 {
1951 struct tpacket_req req;
1952
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001953 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001955 if (pkt_sk(sk)->has_vnet_hdr)
1956 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001957 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07001959 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 case PACKET_COPY_THRESH:
1962 {
1963 int val;
1964
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001965 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001967 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return -EFAULT;
1969
1970 pkt_sk(sk)->copy_thresh = val;
1971 return 0;
1972 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001973 case PACKET_VERSION:
1974 {
1975 int val;
1976
1977 if (optlen != sizeof(val))
1978 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07001979 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001980 return -EBUSY;
1981 if (copy_from_user(&val, optval, sizeof(val)))
1982 return -EFAULT;
1983 switch (val) {
1984 case TPACKET_V1:
1985 case TPACKET_V2:
1986 po->tp_version = val;
1987 return 0;
1988 default:
1989 return -EINVAL;
1990 }
1991 }
Patrick McHardy89133362008-07-18 18:05:19 -07001992 case PACKET_RESERVE:
1993 {
1994 unsigned int val;
1995
1996 if (optlen != sizeof(val))
1997 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07001998 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy89133362008-07-18 18:05:19 -07001999 return -EBUSY;
2000 if (copy_from_user(&val, optval, sizeof(val)))
2001 return -EFAULT;
2002 po->tp_reserve = val;
2003 return 0;
2004 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002005 case PACKET_LOSS:
2006 {
2007 unsigned int val;
2008
2009 if (optlen != sizeof(val))
2010 return -EINVAL;
2011 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2012 return -EBUSY;
2013 if (copy_from_user(&val, optval, sizeof(val)))
2014 return -EFAULT;
2015 po->tp_loss = !!val;
2016 return 0;
2017 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002018 case PACKET_AUXDATA:
2019 {
2020 int val;
2021
2022 if (optlen < sizeof(val))
2023 return -EINVAL;
2024 if (copy_from_user(&val, optval, sizeof(val)))
2025 return -EFAULT;
2026
2027 po->auxdata = !!val;
2028 return 0;
2029 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002030 case PACKET_ORIGDEV:
2031 {
2032 int val;
2033
2034 if (optlen < sizeof(val))
2035 return -EINVAL;
2036 if (copy_from_user(&val, optval, sizeof(val)))
2037 return -EFAULT;
2038
2039 po->origdev = !!val;
2040 return 0;
2041 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002042 case PACKET_VNET_HDR:
2043 {
2044 int val;
2045
2046 if (sock->type != SOCK_RAW)
2047 return -EINVAL;
2048 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2049 return -EBUSY;
2050 if (optlen < sizeof(val))
2051 return -EINVAL;
2052 if (copy_from_user(&val, optval, sizeof(val)))
2053 return -EFAULT;
2054
2055 po->has_vnet_hdr = !!val;
2056 return 0;
2057 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002058 case PACKET_TIMESTAMP:
2059 {
2060 int val;
2061
2062 if (optlen != sizeof(val))
2063 return -EINVAL;
2064 if (copy_from_user(&val, optval, sizeof(val)))
2065 return -EFAULT;
2066
2067 po->tp_tstamp = val;
2068 return 0;
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 default:
2071 return -ENOPROTOOPT;
2072 }
2073}
2074
2075static int packet_getsockopt(struct socket *sock, int level, int optname,
2076 char __user *optval, int __user *optlen)
2077{
2078 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002079 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 struct sock *sk = sock->sk;
2081 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002082 void *data;
2083 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if (level != SOL_PACKET)
2086 return -ENOPROTOOPT;
2087
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002088 if (get_user(len, optlen))
2089 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (len < 0)
2092 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002093
Johann Baudy69e3c752009-05-18 22:11:22 -07002094 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (len > sizeof(struct tpacket_stats))
2097 len = sizeof(struct tpacket_stats);
2098 spin_lock_bh(&sk->sk_receive_queue.lock);
2099 st = po->stats;
2100 memset(&po->stats, 0, sizeof(st));
2101 spin_unlock_bh(&sk->sk_receive_queue.lock);
2102 st.tp_packets += st.tp_drops;
2103
Herbert Xu8dc41942007-02-04 23:31:32 -08002104 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002106 case PACKET_AUXDATA:
2107 if (len > sizeof(int))
2108 len = sizeof(int);
2109 val = po->auxdata;
2110
2111 data = &val;
2112 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002113 case PACKET_ORIGDEV:
2114 if (len > sizeof(int))
2115 len = sizeof(int);
2116 val = po->origdev;
2117
2118 data = &val;
2119 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002120 case PACKET_VNET_HDR:
2121 if (len > sizeof(int))
2122 len = sizeof(int);
2123 val = po->has_vnet_hdr;
2124
2125 data = &val;
2126 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002127 case PACKET_VERSION:
2128 if (len > sizeof(int))
2129 len = sizeof(int);
2130 val = po->tp_version;
2131 data = &val;
2132 break;
2133 case PACKET_HDRLEN:
2134 if (len > sizeof(int))
2135 len = sizeof(int);
2136 if (copy_from_user(&val, optval, len))
2137 return -EFAULT;
2138 switch (val) {
2139 case TPACKET_V1:
2140 val = sizeof(struct tpacket_hdr);
2141 break;
2142 case TPACKET_V2:
2143 val = sizeof(struct tpacket2_hdr);
2144 break;
2145 default:
2146 return -EINVAL;
2147 }
2148 data = &val;
2149 break;
Patrick McHardy89133362008-07-18 18:05:19 -07002150 case PACKET_RESERVE:
2151 if (len > sizeof(unsigned int))
2152 len = sizeof(unsigned int);
2153 val = po->tp_reserve;
2154 data = &val;
2155 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002156 case PACKET_LOSS:
2157 if (len > sizeof(unsigned int))
2158 len = sizeof(unsigned int);
2159 val = po->tp_loss;
2160 data = &val;
2161 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002162 case PACKET_TIMESTAMP:
2163 if (len > sizeof(int))
2164 len = sizeof(int);
2165 val = po->tp_tstamp;
2166 data = &val;
2167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 default:
2169 return -ENOPROTOOPT;
2170 }
2171
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002172 if (put_user(len, optlen))
2173 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002174 if (copy_to_user(optval, data, len))
2175 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179
2180static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2181{
2182 struct sock *sk;
2183 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002184 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002185 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
stephen hemminger808f5112010-02-22 07:57:18 +00002187 rcu_read_lock();
2188 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct packet_sock *po = pkt_sk(sk);
2190
2191 switch (msg) {
2192 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (po->mclist)
2194 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002195 /* fallthrough */
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 case NETDEV_DOWN:
2198 if (dev->ifindex == po->ifindex) {
2199 spin_lock(&po->bind_lock);
2200 if (po->running) {
2201 __dev_remove_pack(&po->prot_hook);
2202 __sock_put(sk);
2203 po->running = 0;
2204 sk->sk_err = ENETDOWN;
2205 if (!sock_flag(sk, SOCK_DEAD))
2206 sk->sk_error_report(sk);
2207 }
2208 if (msg == NETDEV_UNREGISTER) {
2209 po->ifindex = -1;
2210 po->prot_hook.dev = NULL;
2211 }
2212 spin_unlock(&po->bind_lock);
2213 }
2214 break;
2215 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002216 if (dev->ifindex == po->ifindex) {
2217 spin_lock(&po->bind_lock);
2218 if (po->num && !po->running) {
2219 dev_add_pack(&po->prot_hook);
2220 sock_hold(sk);
2221 po->running = 1;
2222 }
2223 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 break;
2226 }
2227 }
stephen hemminger808f5112010-02-22 07:57:18 +00002228 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return NOTIFY_DONE;
2230}
2231
2232
2233static int packet_ioctl(struct socket *sock, unsigned int cmd,
2234 unsigned long arg)
2235{
2236 struct sock *sk = sock->sk;
2237
Johann Baudy69e3c752009-05-18 22:11:22 -07002238 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002239 case SIOCOUTQ:
2240 {
2241 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002242
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002243 return put_user(amount, (int __user *)arg);
2244 }
2245 case SIOCINQ:
2246 {
2247 struct sk_buff *skb;
2248 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002250 spin_lock_bh(&sk->sk_receive_queue.lock);
2251 skb = skb_peek(&sk->sk_receive_queue);
2252 if (skb)
2253 amount = skb->len;
2254 spin_unlock_bh(&sk->sk_receive_queue.lock);
2255 return put_user(amount, (int __user *)arg);
2256 }
2257 case SIOCGSTAMP:
2258 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2259 case SIOCGSTAMPNS:
2260 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002263 case SIOCADDRT:
2264 case SIOCDELRT:
2265 case SIOCDARP:
2266 case SIOCGARP:
2267 case SIOCSARP:
2268 case SIOCGIFADDR:
2269 case SIOCSIFADDR:
2270 case SIOCGIFBRDADDR:
2271 case SIOCSIFBRDADDR:
2272 case SIOCGIFNETMASK:
2273 case SIOCSIFNETMASK:
2274 case SIOCGIFDSTADDR:
2275 case SIOCSIFDSTADDR:
2276 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002277 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278#endif
2279
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002280 default:
2281 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283 return 0;
2284}
2285
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002286static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 poll_table *wait)
2288{
2289 struct sock *sk = sock->sk;
2290 struct packet_sock *po = pkt_sk(sk);
2291 unsigned int mask = datagram_poll(file, sock, wait);
2292
2293 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002294 if (po->rx_ring.pg_vec) {
2295 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 mask |= POLLIN | POLLRDNORM;
2297 }
2298 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002299 spin_lock_bh(&sk->sk_write_queue.lock);
2300 if (po->tx_ring.pg_vec) {
2301 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2302 mask |= POLLOUT | POLLWRNORM;
2303 }
2304 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return mask;
2306}
2307
2308
2309/* Dirty? Well, I still did not learn better way to account
2310 * for user mmaps.
2311 */
2312
2313static void packet_mm_open(struct vm_area_struct *vma)
2314{
2315 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002316 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (sk)
2320 atomic_inc(&pkt_sk(sk)->mapped);
2321}
2322
2323static void packet_mm_close(struct vm_area_struct *vma)
2324{
2325 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002326 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (sk)
2330 atomic_dec(&pkt_sk(sk)->mapped);
2331}
2332
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002333static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002334 .open = packet_mm_open,
2335 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336};
2337
Neil Horman0e3125c2010-11-16 10:26:47 -08002338static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2339 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 int i;
2342
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002343 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002344 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002345 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002346 vfree(pg_vec[i].buffer);
2347 else
2348 free_pages((unsigned long)pg_vec[i].buffer,
2349 order);
2350 pg_vec[i].buffer = NULL;
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353 kfree(pg_vec);
2354}
2355
Changli Gaoc56b4d92010-12-01 02:52:57 +00002356static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002357{
Neil Horman0e3125c2010-11-16 10:26:47 -08002358 char *buffer = NULL;
2359 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2360 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002361
Neil Horman0e3125c2010-11-16 10:26:47 -08002362 buffer = (char *) __get_free_pages(gfp_flags, order);
2363
2364 if (buffer)
2365 return buffer;
2366
2367 /*
2368 * __get_free_pages failed, fall back to vmalloc
2369 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002370 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002371
2372 if (buffer)
2373 return buffer;
2374
2375 /*
2376 * vmalloc failed, lets dig into swap here
2377 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002378 gfp_flags &= ~__GFP_NORETRY;
2379 buffer = (char *)__get_free_pages(gfp_flags, order);
2380 if (buffer)
2381 return buffer;
2382
2383 /*
2384 * complete and utter failure
2385 */
2386 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002387}
2388
Neil Horman0e3125c2010-11-16 10:26:47 -08002389static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002390{
2391 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002392 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002393 int i;
2394
Neil Horman0e3125c2010-11-16 10:26:47 -08002395 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002396 if (unlikely(!pg_vec))
2397 goto out;
2398
2399 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002400 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002401 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002402 goto out_free_pgvec;
2403 }
2404
2405out:
2406 return pg_vec;
2407
2408out_free_pgvec:
2409 free_pg_vec(pg_vec, order, block_nr);
2410 pg_vec = NULL;
2411 goto out;
2412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Johann Baudy69e3c752009-05-18 22:11:22 -07002414static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2415 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Neil Horman0e3125c2010-11-16 10:26:47 -08002417 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002419 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002420 struct packet_ring_buffer *rb;
2421 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002422 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002423 int err;
2424
2425 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2426 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2427
2428 err = -EBUSY;
2429 if (!closing) {
2430 if (atomic_read(&po->mapped))
2431 goto out;
2432 if (atomic_read(&rb->pending))
2433 goto out;
2434 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002438 err = -EBUSY;
2439 if (unlikely(rb->pg_vec))
2440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002442 switch (po->tp_version) {
2443 case TPACKET_V1:
2444 po->tp_hdrlen = TPACKET_HDRLEN;
2445 break;
2446 case TPACKET_V2:
2447 po->tp_hdrlen = TPACKET2_HDRLEN;
2448 break;
2449 }
2450
Johann Baudy69e3c752009-05-18 22:11:22 -07002451 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002452 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002453 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002454 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002455 goto out;
Patrick McHardy89133362008-07-18 18:05:19 -07002456 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002457 po->tp_reserve))
2458 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002459 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Johann Baudy69e3c752009-05-18 22:11:22 -07002462 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2463 if (unlikely(rb->frames_per_block <= 0))
2464 goto out;
2465 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2466 req->tp_frame_nr))
2467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002470 order = get_order(req->tp_block_size);
2471 pg_vec = alloc_pg_vec(req, order);
2472 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002474 }
2475 /* Done */
2476 else {
2477 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002478 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481
2482 lock_sock(sk);
2483
2484 /* Detach socket from network */
2485 spin_lock(&po->bind_lock);
2486 was_running = po->running;
2487 num = po->num;
2488 if (was_running) {
2489 __dev_remove_pack(&po->prot_hook);
2490 po->num = 0;
2491 po->running = 0;
2492 __sock_put(sk);
2493 }
2494 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 synchronize_net();
2497
2498 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002499 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (closing || atomic_read(&po->mapped) == 0) {
2501 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002502 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002503 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002504 rb->frame_max = (req->tp_frame_nr - 1);
2505 rb->head = 0;
2506 rb->frame_size = req->tp_frame_size;
2507 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Changli Gaoc053fd92010-12-10 16:02:20 -08002509 swap(rb->pg_vec_order, order);
2510 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Johann Baudy69e3c752009-05-18 22:11:22 -07002512 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2513 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2514 tpacket_rcv : packet_rcv;
2515 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002517 pr_err("packet_mmap: vma is busy: %d\n",
2518 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Herbert Xu905db442009-01-30 14:12:06 -08002520 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 spin_lock(&po->bind_lock);
2523 if (was_running && !po->running) {
2524 sock_hold(sk);
2525 po->running = 1;
2526 po->num = num;
2527 dev_add_pack(&po->prot_hook);
2528 }
2529 spin_unlock(&po->bind_lock);
2530
2531 release_sock(sk);
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (pg_vec)
2534 free_pg_vec(pg_vec, order, req->tp_block_nr);
2535out:
2536 return err;
2537}
2538
Johann Baudy69e3c752009-05-18 22:11:22 -07002539static int packet_mmap(struct file *file, struct socket *sock,
2540 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 struct sock *sk = sock->sk;
2543 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002544 unsigned long size, expected_size;
2545 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 unsigned long start;
2547 int err = -EINVAL;
2548 int i;
2549
2550 if (vma->vm_pgoff)
2551 return -EINVAL;
2552
Herbert Xu905db442009-01-30 14:12:06 -08002553 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002554
2555 expected_size = 0;
2556 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2557 if (rb->pg_vec) {
2558 expected_size += rb->pg_vec_len
2559 * rb->pg_vec_pages
2560 * PAGE_SIZE;
2561 }
2562 }
2563
2564 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002566
2567 size = vma->vm_end - vma->vm_start;
2568 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 goto out;
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002572 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2573 if (rb->pg_vec == NULL)
2574 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002575
Johann Baudy69e3c752009-05-18 22:11:22 -07002576 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002577 struct page *page;
2578 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002579 int pg_num;
2580
Changli Gaoc56b4d92010-12-01 02:52:57 +00002581 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2582 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002583 err = vm_insert_page(vma, start, page);
2584 if (unlikely(err))
2585 goto out;
2586 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002587 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002588 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002591
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002592 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 vma->vm_ops = &packet_mmap_ops;
2594 err = 0;
2595
2596out:
Herbert Xu905db442009-01-30 14:12:06 -08002597 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return err;
2599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002601static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 .family = PF_PACKET,
2603 .owner = THIS_MODULE,
2604 .release = packet_release,
2605 .bind = packet_bind_spkt,
2606 .connect = sock_no_connect,
2607 .socketpair = sock_no_socketpair,
2608 .accept = sock_no_accept,
2609 .getname = packet_getname_spkt,
2610 .poll = datagram_poll,
2611 .ioctl = packet_ioctl,
2612 .listen = sock_no_listen,
2613 .shutdown = sock_no_shutdown,
2614 .setsockopt = sock_no_setsockopt,
2615 .getsockopt = sock_no_getsockopt,
2616 .sendmsg = packet_sendmsg_spkt,
2617 .recvmsg = packet_recvmsg,
2618 .mmap = sock_no_mmap,
2619 .sendpage = sock_no_sendpage,
2620};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002622static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 .family = PF_PACKET,
2624 .owner = THIS_MODULE,
2625 .release = packet_release,
2626 .bind = packet_bind,
2627 .connect = sock_no_connect,
2628 .socketpair = sock_no_socketpair,
2629 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002630 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 .poll = packet_poll,
2632 .ioctl = packet_ioctl,
2633 .listen = sock_no_listen,
2634 .shutdown = sock_no_shutdown,
2635 .setsockopt = packet_setsockopt,
2636 .getsockopt = packet_getsockopt,
2637 .sendmsg = packet_sendmsg,
2638 .recvmsg = packet_recvmsg,
2639 .mmap = packet_mmap,
2640 .sendpage = sock_no_sendpage,
2641};
2642
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002643static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .family = PF_PACKET,
2645 .create = packet_create,
2646 .owner = THIS_MODULE,
2647};
2648
2649static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002650 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651};
2652
2653#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00002656 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Denis V. Luneve372c412007-11-19 22:31:54 -08002658 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002659
2660 rcu_read_lock();
2661 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
2664static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2665{
Herbert Xu1bf40952007-12-16 14:04:02 -08002666 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002667 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
2670static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00002671 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
stephen hemminger808f5112010-02-22 07:57:18 +00002673 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002676static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 if (v == SEQ_START_TOKEN)
2679 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2680 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00002681 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 const struct packet_sock *po = pkt_sk(s);
2683
2684 seq_printf(seq,
2685 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2686 s,
2687 atomic_read(&s->sk_refcnt),
2688 s->sk_type,
2689 ntohs(po->num),
2690 po->ifindex,
2691 po->running,
2692 atomic_read(&s->sk_rmem_alloc),
2693 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002694 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696
2697 return 0;
2698}
2699
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002700static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 .start = packet_seq_start,
2702 .next = packet_seq_next,
2703 .stop = packet_seq_stop,
2704 .show = packet_seq_show,
2705};
2706
2707static int packet_seq_open(struct inode *inode, struct file *file)
2708{
Denis V. Luneve372c412007-11-19 22:31:54 -08002709 return seq_open_net(inode, file, &packet_seq_ops,
2710 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
Arjan van de Venda7071d2007-02-12 00:55:36 -08002713static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 .owner = THIS_MODULE,
2715 .open = packet_seq_open,
2716 .read = seq_read,
2717 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002718 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719};
2720
2721#endif
2722
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002723static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002724{
stephen hemminger808f5112010-02-22 07:57:18 +00002725 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08002726 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002727
2728 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
2729 return -ENOMEM;
2730
2731 return 0;
2732}
2733
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002734static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002735{
2736 proc_net_remove(net, "packet");
2737}
2738
2739static struct pernet_operations packet_net_ops = {
2740 .init = packet_net_init,
2741 .exit = packet_net_exit,
2742};
2743
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745static void __exit packet_exit(void)
2746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002748 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 sock_unregister(PF_PACKET);
2750 proto_unregister(&packet_proto);
2751}
2752
2753static int __init packet_init(void)
2754{
2755 int rc = proto_register(&packet_proto, 0);
2756
2757 if (rc != 0)
2758 goto out;
2759
2760 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002761 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763out:
2764 return rc;
2765}
2766
2767module_init(packet_init);
2768module_exit(packet_exit);
2769MODULE_LICENSE("GPL");
2770MODULE_ALIAS_NETPROTO(PF_PACKET);