Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 8 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 10 | * Alan Cox, <gw4pts@gw4pts.ampr.org> |
| 11 | * |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 12 | * Fixes: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * 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 Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 35 | * Ulises Alonso : Frame number limit removal and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | * packet_set_ring memory leak. |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 37 | * Eric Biederman : Allow for > 8 byte hardware addresses. |
| 38 | * The convention is that longer addresses |
| 39 | * will simply extend the hardware address |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 40 | * byte arrays at the end of sockaddr_ll |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 41 | * and packet_mreq. |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 42 | * Johann Baudy : Added TX RING. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * |
| 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 Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/mm.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 53 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #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 Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 61 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #include <linux/kmod.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 63 | #include <linux/slab.h> |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 64 | #include <linux/vmalloc.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 65 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #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 Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 76 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #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 Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 83 | #include <linux/mutex.h> |
Eric Dumazet | 05423b2 | 2009-10-26 18:40:35 -0700 | [diff] [blame] | 84 | #include <linux/if_vlan.h> |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 85 | #include <linux/virtio_net.h> |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 86 | #include <linux/errqueue.h> |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 87 | #include <linux/net_tstamp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | #ifdef CONFIG_INET |
| 90 | #include <net/inet_common.h> |
| 91 | #endif |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | 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 | |
| 104 | On receive: |
| 105 | ----------- |
| 106 | |
| 107 | Incoming, dev->hard_header!=NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 108 | mac_header -> ll header |
| 109 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | Outgoing, dev->hard_header!=NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 112 | mac_header -> ll header |
| 113 | data -> ll header |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | Incoming, dev->hard_header==NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 116 | mac_header -> UNKNOWN position. It is very likely, that it points to ll |
| 117 | header. PPP makes it, that is wrong, because introduce |
YOSHIFUJI Hideaki | db0c58f | 2007-07-19 10:44:35 +0900 | [diff] [blame] | 118 | assymetry between rx and tx paths. |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 119 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | Outgoing, dev->hard_header==NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 122 | mac_header -> data. ll header is still not built! |
| 123 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | Resume |
| 126 | If dev->hard_header==NULL we are unlikely to restore sensible ll header. |
| 127 | |
| 128 | |
| 129 | On transmit: |
| 130 | ------------ |
| 131 | |
| 132 | dev->hard_header != NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 133 | mac_header -> ll header |
| 134 | data -> ll header |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | dev->hard_header == NULL (ll header is added by device, we cannot control it) |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 137 | mac_header -> data |
| 138 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | We should set nh.raw on output to correct posistion, |
| 141 | packet classifier depends on it. |
| 142 | */ |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | /* Private packet socket structures. */ |
| 145 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 146 | struct packet_mclist { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | struct packet_mclist *next; |
| 148 | int ifindex; |
| 149 | int count; |
| 150 | unsigned short type; |
| 151 | unsigned short alen; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 152 | unsigned char addr[MAX_ADDR_LEN]; |
| 153 | }; |
| 154 | /* identical to struct packet_mreq except it has |
| 155 | * a longer address field. |
| 156 | */ |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 157 | struct packet_mreq_max { |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 158 | int mr_ifindex; |
| 159 | unsigned short mr_type; |
| 160 | unsigned short mr_alen; |
| 161 | unsigned char mr_address[MAX_ADDR_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | }; |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 163 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 164 | static int packet_set_ring(struct sock *sk, struct tpacket_req *req, |
| 165 | int closing, int tx_ring); |
| 166 | |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 167 | struct pgv { |
| 168 | char *buffer; |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 171 | struct packet_ring_buffer { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 172 | struct pgv *pg_vec; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 173 | 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 | |
| 185 | struct packet_sock; |
| 186 | static int tpacket_snd(struct packet_sock *po, struct msghdr *msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | static void packet_flush_mclist(struct sock *sk); |
| 189 | |
| 190 | struct packet_sock { |
| 191 | /* struct sock has to be the first member of packet_sock */ |
| 192 | struct sock sk; |
| 193 | struct tpacket_stats stats; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 194 | struct packet_ring_buffer rx_ring; |
| 195 | struct packet_ring_buffer tx_ring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | int copy_thresh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | spinlock_t bind_lock; |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 198 | struct mutex pg_vec_lock; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 199 | unsigned int running:1, /* prot_hook is attached*/ |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 200 | auxdata:1, |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 201 | origdev:1, |
| 202 | has_vnet_hdr:1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | int ifindex; /* bound device */ |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 204 | __be16 num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | struct packet_mclist *mclist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | atomic_t mapped; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 207 | enum tpacket_versions tp_version; |
| 208 | unsigned int tp_hdrlen; |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 209 | unsigned int tp_reserve; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 210 | unsigned int tp_loss:1; |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 211 | unsigned int tp_tstamp; |
Eric Dumazet | 94b05952 | 2009-10-16 04:02:20 +0000 | [diff] [blame] | 212 | struct packet_type prot_hook ____cacheline_aligned_in_smp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 215 | struct 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 Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 224 | |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 225 | static inline struct packet_sock *pkt_sk(struct sock *sk) |
| 226 | { |
| 227 | return (struct packet_sock *)sk; |
| 228 | } |
| 229 | |
| 230 | /* register_prot_hook must be invoked with the po->bind_lock held, |
| 231 | * or from a context in which asynchronous accesses to the packet |
| 232 | * socket is not possible (packet_create()). |
| 233 | */ |
| 234 | static void register_prot_hook(struct sock *sk) |
| 235 | { |
| 236 | struct packet_sock *po = pkt_sk(sk); |
| 237 | if (!po->running) { |
| 238 | dev_add_pack(&po->prot_hook); |
| 239 | sock_hold(sk); |
| 240 | po->running = 1; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* {,__}unregister_prot_hook() must be invoked with the po->bind_lock |
| 245 | * held. If the sync parameter is true, we will temporarily drop |
| 246 | * the po->bind_lock and do a synchronize_net to make sure no |
| 247 | * asynchronous packet processing paths still refer to the elements |
| 248 | * of po->prot_hook. If the sync parameter is false, it is the |
| 249 | * callers responsibility to take care of this. |
| 250 | */ |
| 251 | static void __unregister_prot_hook(struct sock *sk, bool sync) |
| 252 | { |
| 253 | struct packet_sock *po = pkt_sk(sk); |
| 254 | |
| 255 | po->running = 0; |
| 256 | __dev_remove_pack(&po->prot_hook); |
| 257 | __sock_put(sk); |
| 258 | |
| 259 | if (sync) { |
| 260 | spin_unlock(&po->bind_lock); |
| 261 | synchronize_net(); |
| 262 | spin_lock(&po->bind_lock); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static void unregister_prot_hook(struct sock *sk, bool sync) |
| 267 | { |
| 268 | struct packet_sock *po = pkt_sk(sk); |
| 269 | |
| 270 | if (po->running) |
| 271 | __unregister_prot_hook(sk, sync); |
| 272 | } |
| 273 | |
Changli Gao | f6dafa9 | 2010-12-07 04:26:16 +0000 | [diff] [blame] | 274 | static inline __pure struct page *pgv_to_page(void *addr) |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 275 | { |
| 276 | if (is_vmalloc_addr(addr)) |
| 277 | return vmalloc_to_page(addr); |
| 278 | return virt_to_page(addr); |
| 279 | } |
| 280 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 281 | static void __packet_set_status(struct packet_sock *po, void *frame, int status) |
| 282 | { |
| 283 | union { |
| 284 | struct tpacket_hdr *h1; |
| 285 | struct tpacket2_hdr *h2; |
| 286 | void *raw; |
| 287 | } h; |
| 288 | |
| 289 | h.raw = frame; |
| 290 | switch (po->tp_version) { |
| 291 | case TPACKET_V1: |
| 292 | h.h1->tp_status = status; |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 293 | flush_dcache_page(pgv_to_page(&h.h1->tp_status)); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 294 | break; |
| 295 | case TPACKET_V2: |
| 296 | h.h2->tp_status = status; |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 297 | flush_dcache_page(pgv_to_page(&h.h2->tp_status)); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 298 | break; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 299 | default: |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 300 | pr_err("TPACKET version not supported\n"); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 301 | BUG(); |
| 302 | } |
| 303 | |
| 304 | smp_wmb(); |
| 305 | } |
| 306 | |
| 307 | static int __packet_get_status(struct packet_sock *po, void *frame) |
| 308 | { |
| 309 | union { |
| 310 | struct tpacket_hdr *h1; |
| 311 | struct tpacket2_hdr *h2; |
| 312 | void *raw; |
| 313 | } h; |
| 314 | |
| 315 | smp_rmb(); |
| 316 | |
| 317 | h.raw = frame; |
| 318 | switch (po->tp_version) { |
| 319 | case TPACKET_V1: |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 320 | flush_dcache_page(pgv_to_page(&h.h1->tp_status)); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 321 | return h.h1->tp_status; |
| 322 | case TPACKET_V2: |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 323 | flush_dcache_page(pgv_to_page(&h.h2->tp_status)); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 324 | return h.h2->tp_status; |
| 325 | default: |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 326 | pr_err("TPACKET version not supported\n"); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 327 | BUG(); |
| 328 | return 0; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 331 | |
| 332 | static void *packet_lookup_frame(struct packet_sock *po, |
| 333 | struct packet_ring_buffer *rb, |
| 334 | unsigned int position, |
| 335 | int status) |
| 336 | { |
| 337 | unsigned int pg_vec_pos, frame_offset; |
| 338 | union { |
| 339 | struct tpacket_hdr *h1; |
| 340 | struct tpacket2_hdr *h2; |
| 341 | void *raw; |
| 342 | } h; |
| 343 | |
| 344 | pg_vec_pos = position / rb->frames_per_block; |
| 345 | frame_offset = position % rb->frames_per_block; |
| 346 | |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 347 | h.raw = rb->pg_vec[pg_vec_pos].buffer + |
| 348 | (frame_offset * rb->frame_size); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 349 | |
| 350 | if (status != __packet_get_status(po, h.raw)) |
| 351 | return NULL; |
| 352 | |
| 353 | return h.raw; |
| 354 | } |
| 355 | |
| 356 | static inline void *packet_current_frame(struct packet_sock *po, |
| 357 | struct packet_ring_buffer *rb, |
| 358 | int status) |
| 359 | { |
| 360 | return packet_lookup_frame(po, rb, rb->head, status); |
| 361 | } |
| 362 | |
| 363 | static inline void *packet_previous_frame(struct packet_sock *po, |
| 364 | struct packet_ring_buffer *rb, |
| 365 | int status) |
| 366 | { |
| 367 | unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max; |
| 368 | return packet_lookup_frame(po, rb, previous, status); |
| 369 | } |
| 370 | |
| 371 | static inline void packet_increment_head(struct packet_ring_buffer *buff) |
| 372 | { |
| 373 | buff->head = buff->head != buff->frame_max ? buff->head+1 : 0; |
| 374 | } |
| 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | static void packet_sock_destruct(struct sock *sk) |
| 377 | { |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 378 | skb_queue_purge(&sk->sk_error_queue); |
| 379 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 380 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); |
| 381 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | if (!sock_flag(sk, SOCK_DEAD)) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 384 | pr_err("Attempt to release alive packet socket: %p\n", sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
Pavel Emelyanov | 17ab56a | 2007-11-10 21:38:48 -0800 | [diff] [blame] | 388 | sk_refcnt_debug_dec(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 392 | static const struct proto_ops packet_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 394 | static const struct proto_ops packet_ops_spkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 396 | static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, |
| 397 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | struct sock *sk; |
| 400 | struct sockaddr_pkt *spkt; |
| 401 | |
| 402 | /* |
| 403 | * When we registered the protocol we saved the socket in the data |
| 404 | * field for just this event. |
| 405 | */ |
| 406 | |
| 407 | sk = pt->af_packet_priv; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | /* |
| 410 | * Yank back the headers [hope the device set this |
| 411 | * right or kerboom...] |
| 412 | * |
| 413 | * Incoming packets have ll header pulled, |
| 414 | * push it back. |
| 415 | * |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 416 | * For outgoing ones skb->data == skb_mac_header(skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | * so that this procedure is noop. |
| 418 | */ |
| 419 | |
| 420 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 421 | goto out; |
| 422 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 423 | if (!net_eq(dev_net(dev), sock_net(sk))) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 424 | goto out; |
| 425 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 426 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 427 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | goto oom; |
| 429 | |
| 430 | /* drop any routing info */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 431 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 433 | /* drop conntrack reference */ |
| 434 | nf_reset(skb); |
| 435 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 436 | spkt = &PACKET_SKB_CB(skb)->sa.pkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 438 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | /* |
| 441 | * The SOCK_PACKET socket receives _all_ frames. |
| 442 | */ |
| 443 | |
| 444 | spkt->spkt_family = dev->type; |
| 445 | strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device)); |
| 446 | spkt->spkt_protocol = skb->protocol; |
| 447 | |
| 448 | /* |
| 449 | * Charge the memory to the socket. This is done specifically |
| 450 | * to prevent sockets using all the memory up. |
| 451 | */ |
| 452 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 453 | if (sock_queue_rcv_skb(sk, skb) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | return 0; |
| 455 | |
| 456 | out: |
| 457 | kfree_skb(skb); |
| 458 | oom: |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | /* |
| 464 | * Output a raw packet to a device layer. This bypasses all the other |
| 465 | * protocol layers and you must therefore supply it with a complete frame |
| 466 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock, |
| 469 | struct msghdr *msg, size_t len) |
| 470 | { |
| 471 | struct sock *sk = sock->sk; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 472 | struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name; |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 473 | struct sk_buff *skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | struct net_device *dev; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 475 | __be16 proto = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | int err; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 479 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | */ |
| 481 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 482 | if (saddr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (msg->msg_namelen < sizeof(struct sockaddr)) |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 484 | return -EINVAL; |
| 485 | if (msg->msg_namelen == sizeof(struct sockaddr_pkt)) |
| 486 | proto = saddr->spkt_protocol; |
| 487 | } else |
| 488 | return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 491 | * Find the device first to size check it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | */ |
| 493 | |
| 494 | saddr->spkt_device[13] = 0; |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 495 | retry: |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 496 | rcu_read_lock(); |
| 497 | dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | err = -ENODEV; |
| 499 | if (dev == NULL) |
| 500 | goto out_unlock; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 501 | |
David S. Miller | d5e76b0 | 2007-01-25 19:30:36 -0800 | [diff] [blame] | 502 | err = -ENETDOWN; |
| 503 | if (!(dev->flags & IFF_UP)) |
| 504 | goto out_unlock; |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | /* |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 507 | * You may not queue a frame bigger than the mtu. This is the lowest level |
| 508 | * raw protocol and you must do your own fragmentation at this level. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | err = -EMSGSIZE; |
Ben Greear | 57f89bf | 2011-02-11 09:35:18 +0000 | [diff] [blame] | 512 | if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | goto out_unlock; |
| 514 | |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 515 | if (!skb) { |
| 516 | size_t reserved = LL_RESERVED_SPACE(dev); |
| 517 | unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 519 | rcu_read_unlock(); |
| 520 | skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL); |
| 521 | if (skb == NULL) |
| 522 | return -ENOBUFS; |
| 523 | /* FIXME: Save some space for broken drivers that write a hard |
| 524 | * header at transmission time by themselves. PPP is the notable |
| 525 | * one here. This should really be fixed at the driver level. |
| 526 | */ |
| 527 | skb_reserve(skb, reserved); |
| 528 | skb_reset_network_header(skb); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 529 | |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 530 | /* Try to align data part correctly */ |
| 531 | if (hhlen) { |
| 532 | skb->data -= hhlen; |
| 533 | skb->tail -= hhlen; |
| 534 | if (len < hhlen) |
| 535 | skb_reset_network_header(skb); |
| 536 | } |
| 537 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); |
| 538 | if (err) |
| 539 | goto out_free; |
| 540 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Ben Greear | 57f89bf | 2011-02-11 09:35:18 +0000 | [diff] [blame] | 543 | if (len > (dev->mtu + dev->hard_header_len)) { |
| 544 | /* Earlier code assumed this would be a VLAN pkt, |
| 545 | * double-check this now that we have the actual |
| 546 | * packet in hand. |
| 547 | */ |
| 548 | struct ethhdr *ehdr; |
| 549 | skb_reset_mac_header(skb); |
| 550 | ehdr = eth_hdr(skb); |
| 551 | if (ehdr->h_proto != htons(ETH_P_8021Q)) { |
| 552 | err = -EMSGSIZE; |
| 553 | goto out_unlock; |
| 554 | } |
| 555 | } |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | skb->protocol = proto; |
| 558 | skb->dev = dev; |
| 559 | skb->priority = sk->sk_priority; |
Eric Dumazet | 2d37a18 | 2009-10-01 19:14:46 +0000 | [diff] [blame] | 560 | skb->mark = sk->sk_mark; |
Oliver Hartkopp | 2244d07 | 2010-08-17 08:59:14 +0000 | [diff] [blame] | 561 | err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags); |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 562 | if (err < 0) |
| 563 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | dev_queue_xmit(skb); |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 566 | rcu_read_unlock(); |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 567 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | out_unlock: |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 570 | rcu_read_unlock(); |
Eric Dumazet | 1a35ca8 | 2009-12-15 05:47:03 +0000 | [diff] [blame] | 571 | out_free: |
| 572 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | return err; |
| 574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 576 | static inline unsigned int run_filter(const struct sk_buff *skb, |
| 577 | const struct sock *sk, |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 578 | unsigned int res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | struct sk_filter *filter; |
| 581 | |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 582 | rcu_read_lock(); |
| 583 | filter = rcu_dereference(sk->sk_filter); |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 584 | if (filter != NULL) |
Eric Dumazet | 0a14842 | 2011-04-20 09:27:32 +0000 | [diff] [blame] | 585 | res = SK_RUN_FILTER(filter, skb); |
Eric Dumazet | 80f8f10 | 2011-01-18 07:46:52 +0000 | [diff] [blame] | 586 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 588 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | /* |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 592 | * This function makes lazy skb cloning in hope that most of packets |
| 593 | * are discarded by BPF. |
| 594 | * |
| 595 | * Note tricky part: we DO mangle shared skb! skb->data, skb->len |
| 596 | * and skb->cb are mangled. It works because (and until) packets |
| 597 | * falling here are owned by current CPU. Output packets are cloned |
| 598 | * by dev_queue_xmit_nit(), input packets are processed by net_bh |
| 599 | * sequencially, so that if we return skb to original state on exit, |
| 600 | * we will not harm anyone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | */ |
| 602 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 603 | static int packet_rcv(struct sk_buff *skb, struct net_device *dev, |
| 604 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
| 606 | struct sock *sk; |
| 607 | struct sockaddr_ll *sll; |
| 608 | struct packet_sock *po; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 609 | u8 *skb_head = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | int skb_len = skb->len; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 611 | unsigned int snaplen, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 614 | goto drop; |
| 615 | |
| 616 | sk = pt->af_packet_priv; |
| 617 | po = pkt_sk(sk); |
| 618 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 619 | if (!net_eq(dev_net(dev), sock_net(sk))) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 620 | goto drop; |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | skb->dev = dev; |
| 623 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 624 | if (dev->header_ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | /* The device has an explicit notion of ll header, |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 626 | * exported to higher levels. |
| 627 | * |
| 628 | * Otherwise, the device hides details of its frame |
| 629 | * structure, so that corresponding packet head is |
| 630 | * never delivered to user. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | */ |
| 632 | if (sk->sk_type != SOCK_DGRAM) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 633 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | else if (skb->pkt_type == PACKET_OUTGOING) { |
| 635 | /* Special case: outgoing packets have ll header at head */ |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 636 | skb_pull(skb, skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | snaplen = skb->len; |
| 641 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 642 | res = run_filter(skb, sk, snaplen); |
| 643 | if (!res) |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 644 | goto drop_n_restore; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 645 | if (snaplen > res) |
| 646 | snaplen = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
| 649 | (unsigned)sk->sk_rcvbuf) |
| 650 | goto drop_n_acct; |
| 651 | |
| 652 | if (skb_shared(skb)) { |
| 653 | struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); |
| 654 | if (nskb == NULL) |
| 655 | goto drop_n_acct; |
| 656 | |
| 657 | if (skb_head != skb->data) { |
| 658 | skb->data = skb_head; |
| 659 | skb->len = skb_len; |
| 660 | } |
| 661 | kfree_skb(skb); |
| 662 | skb = nskb; |
| 663 | } |
| 664 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 665 | BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 > |
| 666 | sizeof(skb->cb)); |
| 667 | |
| 668 | sll = &PACKET_SKB_CB(skb)->sa.ll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | sll->sll_family = AF_PACKET; |
| 670 | sll->sll_hatype = dev->type; |
| 671 | sll->sll_protocol = skb->protocol; |
| 672 | sll->sll_pkttype = skb->pkt_type; |
Peter P Waskiewicz Jr | 8032b46 | 2007-11-10 22:03:25 -0800 | [diff] [blame] | 673 | if (unlikely(po->origdev)) |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 674 | sll->sll_ifindex = orig_dev->ifindex; |
| 675 | else |
| 676 | sll->sll_ifindex = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 678 | sll->sll_halen = dev_parse_header(skb, sll->sll_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 680 | PACKET_SKB_CB(skb)->origlen = skb->len; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | if (pskb_trim(skb, snaplen)) |
| 683 | goto drop_n_acct; |
| 684 | |
| 685 | skb_set_owner_r(skb, sk); |
| 686 | skb->dev = NULL; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 687 | skb_dst_drop(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 689 | /* drop conntrack reference */ |
| 690 | nf_reset(skb); |
| 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | spin_lock(&sk->sk_receive_queue.lock); |
| 693 | po->stats.tp_packets++; |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 694 | skb->dropcount = atomic_read(&sk->sk_drops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 696 | spin_unlock(&sk->sk_receive_queue.lock); |
| 697 | sk->sk_data_ready(sk, skb->len); |
| 698 | return 0; |
| 699 | |
| 700 | drop_n_acct: |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 701 | po->stats.tp_drops = atomic_inc_return(&sk->sk_drops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | drop_n_restore: |
| 704 | if (skb_head != skb->data && skb_shared(skb)) { |
| 705 | skb->data = skb_head; |
| 706 | skb->len = skb_len; |
| 707 | } |
| 708 | drop: |
Neil Horman | ead2ceb | 2009-03-11 09:49:55 +0000 | [diff] [blame] | 709 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 713 | static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, |
| 714 | struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | { |
| 716 | struct sock *sk; |
| 717 | struct packet_sock *po; |
| 718 | struct sockaddr_ll *sll; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 719 | union { |
| 720 | struct tpacket_hdr *h1; |
| 721 | struct tpacket2_hdr *h2; |
| 722 | void *raw; |
| 723 | } h; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 724 | u8 *skb_head = skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | int skb_len = skb->len; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 726 | unsigned int snaplen, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 728 | unsigned short macoff, netoff, hdrlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | struct sk_buff *copy_skb = NULL; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 730 | struct timeval tv; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 731 | struct timespec ts; |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 732 | struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
| 734 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 735 | goto drop; |
| 736 | |
| 737 | sk = pt->af_packet_priv; |
| 738 | po = pkt_sk(sk); |
| 739 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 740 | if (!net_eq(dev_net(dev), sock_net(sk))) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 741 | goto drop; |
| 742 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 743 | if (dev->header_ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | if (sk->sk_type != SOCK_DGRAM) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 745 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | else if (skb->pkt_type == PACKET_OUTGOING) { |
| 747 | /* Special case: outgoing packets have ll header at head */ |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 748 | skb_pull(skb, skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 752 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 753 | status |= TP_STATUS_CSUMNOTREADY; |
| 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | snaplen = skb->len; |
| 756 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 757 | res = run_filter(skb, sk, snaplen); |
| 758 | if (!res) |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 759 | goto drop_n_restore; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 760 | if (snaplen > res) |
| 761 | snaplen = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
| 763 | if (sk->sk_type == SOCK_DGRAM) { |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 764 | macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 + |
| 765 | po->tp_reserve; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } else { |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 767 | unsigned maclen = skb_network_offset(skb); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 768 | netoff = TPACKET_ALIGN(po->tp_hdrlen + |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 769 | (maclen < 16 ? 16 : maclen)) + |
| 770 | po->tp_reserve; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | macoff = netoff - maclen; |
| 772 | } |
| 773 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 774 | if (macoff + snaplen > po->rx_ring.frame_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | if (po->copy_thresh && |
| 776 | atomic_read(&sk->sk_rmem_alloc) + skb->truesize < |
| 777 | (unsigned)sk->sk_rcvbuf) { |
| 778 | if (skb_shared(skb)) { |
| 779 | copy_skb = skb_clone(skb, GFP_ATOMIC); |
| 780 | } else { |
| 781 | copy_skb = skb_get(skb); |
| 782 | skb_head = skb->data; |
| 783 | } |
| 784 | if (copy_skb) |
| 785 | skb_set_owner_r(copy_skb, sk); |
| 786 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 787 | snaplen = po->rx_ring.frame_size - macoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if ((int)snaplen < 0) |
| 789 | snaplen = 0; |
| 790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
| 792 | spin_lock(&sk->sk_receive_queue.lock); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 793 | h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL); |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 794 | if (!h.raw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | goto ring_is_full; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 796 | packet_increment_head(&po->rx_ring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | po->stats.tp_packets++; |
| 798 | if (copy_skb) { |
| 799 | status |= TP_STATUS_COPY; |
| 800 | __skb_queue_tail(&sk->sk_receive_queue, copy_skb); |
| 801 | } |
| 802 | if (!po->stats.tp_drops) |
| 803 | status &= ~TP_STATUS_LOSING; |
| 804 | spin_unlock(&sk->sk_receive_queue.lock); |
| 805 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 806 | skb_copy_bits(skb, 0, h.raw + macoff, snaplen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 808 | switch (po->tp_version) { |
| 809 | case TPACKET_V1: |
| 810 | h.h1->tp_len = skb->len; |
| 811 | h.h1->tp_snaplen = snaplen; |
| 812 | h.h1->tp_mac = macoff; |
| 813 | h.h1->tp_net = netoff; |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 814 | if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE) |
| 815 | && shhwtstamps->syststamp.tv64) |
| 816 | tv = ktime_to_timeval(shhwtstamps->syststamp); |
| 817 | else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE) |
| 818 | && shhwtstamps->hwtstamp.tv64) |
| 819 | tv = ktime_to_timeval(shhwtstamps->hwtstamp); |
| 820 | else if (skb->tstamp.tv64) |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 821 | tv = ktime_to_timeval(skb->tstamp); |
| 822 | else |
| 823 | do_gettimeofday(&tv); |
| 824 | h.h1->tp_sec = tv.tv_sec; |
| 825 | h.h1->tp_usec = tv.tv_usec; |
| 826 | hdrlen = sizeof(*h.h1); |
| 827 | break; |
| 828 | case TPACKET_V2: |
| 829 | h.h2->tp_len = skb->len; |
| 830 | h.h2->tp_snaplen = snaplen; |
| 831 | h.h2->tp_mac = macoff; |
| 832 | h.h2->tp_net = netoff; |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 833 | if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE) |
| 834 | && shhwtstamps->syststamp.tv64) |
| 835 | ts = ktime_to_timespec(shhwtstamps->syststamp); |
| 836 | else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE) |
| 837 | && shhwtstamps->hwtstamp.tv64) |
| 838 | ts = ktime_to_timespec(shhwtstamps->hwtstamp); |
| 839 | else if (skb->tstamp.tv64) |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 840 | ts = ktime_to_timespec(skb->tstamp); |
| 841 | else |
| 842 | getnstimeofday(&ts); |
| 843 | h.h2->tp_sec = ts.tv_sec; |
| 844 | h.h2->tp_nsec = ts.tv_nsec; |
Ben Greear | a3bcc23 | 2011-06-01 06:49:10 +0000 | [diff] [blame] | 845 | if (vlan_tx_tag_present(skb)) { |
| 846 | h.h2->tp_vlan_tci = vlan_tx_tag_get(skb); |
| 847 | status |= TP_STATUS_VLAN_VALID; |
| 848 | } else { |
| 849 | h.h2->tp_vlan_tci = 0; |
| 850 | } |
Eric Dumazet | 13fcb7b | 2011-06-06 22:42:06 -0700 | [diff] [blame] | 851 | h.h2->tp_padding = 0; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 852 | hdrlen = sizeof(*h.h2); |
| 853 | break; |
| 854 | default: |
| 855 | BUG(); |
| 856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 858 | sll = h.raw + TPACKET_ALIGN(hdrlen); |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 859 | sll->sll_halen = dev_parse_header(skb, sll->sll_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | sll->sll_family = AF_PACKET; |
| 861 | sll->sll_hatype = dev->type; |
| 862 | sll->sll_protocol = skb->protocol; |
| 863 | sll->sll_pkttype = skb->pkt_type; |
Peter P Waskiewicz Jr | 8032b46 | 2007-11-10 22:03:25 -0800 | [diff] [blame] | 864 | if (unlikely(po->origdev)) |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 865 | sll->sll_ifindex = orig_dev->ifindex; |
| 866 | else |
| 867 | sll->sll_ifindex = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 869 | __packet_set_status(po, h.raw, status); |
Ralf Baechle | e16aa20 | 2006-12-07 00:11:33 -0800 | [diff] [blame] | 870 | smp_mb(); |
Changli Gao | f6dafa9 | 2010-12-07 04:26:16 +0000 | [diff] [blame] | 871 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 873 | u8 *start, *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 875 | end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen); |
| 876 | for (start = h.raw; start < end; start += PAGE_SIZE) |
| 877 | flush_dcache_page(pgv_to_page(start)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
Changli Gao | f6dafa9 | 2010-12-07 04:26:16 +0000 | [diff] [blame] | 879 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
| 881 | sk->sk_data_ready(sk, 0); |
| 882 | |
| 883 | drop_n_restore: |
| 884 | if (skb_head != skb->data && skb_shared(skb)) { |
| 885 | skb->data = skb_head; |
| 886 | skb->len = skb_len; |
| 887 | } |
| 888 | drop: |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 889 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | return 0; |
| 891 | |
| 892 | ring_is_full: |
| 893 | po->stats.tp_drops++; |
| 894 | spin_unlock(&sk->sk_receive_queue.lock); |
| 895 | |
| 896 | sk->sk_data_ready(sk, 0); |
Wei Yongjun | acb5d75 | 2009-02-25 00:36:42 +0000 | [diff] [blame] | 897 | kfree_skb(copy_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | goto drop_n_restore; |
| 899 | } |
| 900 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 901 | static void tpacket_destruct_skb(struct sk_buff *skb) |
| 902 | { |
| 903 | struct packet_sock *po = pkt_sk(skb->sk); |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 904 | void *ph; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 905 | |
| 906 | BUG_ON(skb == NULL); |
| 907 | |
| 908 | if (likely(po->tx_ring.pg_vec)) { |
| 909 | ph = skb_shinfo(skb)->destructor_arg; |
| 910 | BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING); |
| 911 | BUG_ON(atomic_read(&po->tx_ring.pending) == 0); |
| 912 | atomic_dec(&po->tx_ring.pending); |
| 913 | __packet_set_status(po, ph, TP_STATUS_AVAILABLE); |
| 914 | } |
| 915 | |
| 916 | sock_wfree(skb); |
| 917 | } |
| 918 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 919 | static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb, |
| 920 | void *frame, struct net_device *dev, int size_max, |
| 921 | __be16 proto, unsigned char *addr) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 922 | { |
| 923 | union { |
| 924 | struct tpacket_hdr *h1; |
| 925 | struct tpacket2_hdr *h2; |
| 926 | void *raw; |
| 927 | } ph; |
| 928 | int to_write, offset, len, tp_len, nr_frags, len_max; |
| 929 | struct socket *sock = po->sk.sk_socket; |
| 930 | struct page *page; |
| 931 | void *data; |
| 932 | int err; |
| 933 | |
| 934 | ph.raw = frame; |
| 935 | |
| 936 | skb->protocol = proto; |
| 937 | skb->dev = dev; |
| 938 | skb->priority = po->sk.sk_priority; |
Eric Dumazet | 2d37a18 | 2009-10-01 19:14:46 +0000 | [diff] [blame] | 939 | skb->mark = po->sk.sk_mark; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 940 | skb_shinfo(skb)->destructor_arg = ph.raw; |
| 941 | |
| 942 | switch (po->tp_version) { |
| 943 | case TPACKET_V2: |
| 944 | tp_len = ph.h2->tp_len; |
| 945 | break; |
| 946 | default: |
| 947 | tp_len = ph.h1->tp_len; |
| 948 | break; |
| 949 | } |
| 950 | if (unlikely(tp_len > size_max)) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 951 | pr_err("packet size is too long (%d > %d)\n", tp_len, size_max); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 952 | return -EMSGSIZE; |
| 953 | } |
| 954 | |
| 955 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
| 956 | skb_reset_network_header(skb); |
| 957 | |
| 958 | data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll); |
| 959 | to_write = tp_len; |
| 960 | |
| 961 | if (sock->type == SOCK_DGRAM) { |
| 962 | err = dev_hard_header(skb, dev, ntohs(proto), addr, |
| 963 | NULL, tp_len); |
| 964 | if (unlikely(err < 0)) |
| 965 | return -EINVAL; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 966 | } else if (dev->hard_header_len) { |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 967 | /* net device doesn't like empty head */ |
| 968 | if (unlikely(tp_len <= dev->hard_header_len)) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 969 | pr_err("packet size is too short (%d < %d)\n", |
| 970 | tp_len, dev->hard_header_len); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 971 | return -EINVAL; |
| 972 | } |
| 973 | |
| 974 | skb_push(skb, dev->hard_header_len); |
| 975 | err = skb_store_bits(skb, 0, data, |
| 976 | dev->hard_header_len); |
| 977 | if (unlikely(err)) |
| 978 | return err; |
| 979 | |
| 980 | data += dev->hard_header_len; |
| 981 | to_write -= dev->hard_header_len; |
| 982 | } |
| 983 | |
| 984 | err = -EFAULT; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 985 | offset = offset_in_page(data); |
| 986 | len_max = PAGE_SIZE - offset; |
| 987 | len = ((to_write > len_max) ? len_max : to_write); |
| 988 | |
| 989 | skb->data_len = to_write; |
| 990 | skb->len += to_write; |
| 991 | skb->truesize += to_write; |
| 992 | atomic_add(to_write, &po->sk.sk_wmem_alloc); |
| 993 | |
| 994 | while (likely(to_write)) { |
| 995 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 996 | |
| 997 | if (unlikely(nr_frags >= MAX_SKB_FRAGS)) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 998 | pr_err("Packet exceed the number of skb frags(%lu)\n", |
| 999 | MAX_SKB_FRAGS); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1000 | return -EFAULT; |
| 1001 | } |
| 1002 | |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 1003 | page = pgv_to_page(data); |
| 1004 | data += len; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1005 | flush_dcache_page(page); |
| 1006 | get_page(page); |
Changli Gao | 0af55bb | 2010-12-01 02:52:20 +0000 | [diff] [blame] | 1007 | skb_fill_page_desc(skb, nr_frags, page, offset, len); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1008 | to_write -= len; |
| 1009 | offset = 0; |
| 1010 | len_max = PAGE_SIZE; |
| 1011 | len = ((to_write > len_max) ? len_max : to_write); |
| 1012 | } |
| 1013 | |
| 1014 | return tp_len; |
| 1015 | } |
| 1016 | |
| 1017 | static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) |
| 1018 | { |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1019 | struct sk_buff *skb; |
| 1020 | struct net_device *dev; |
| 1021 | __be16 proto; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1022 | bool need_rls_dev = false; |
| 1023 | int err, reserve = 0; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1024 | void *ph; |
| 1025 | struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1026 | int tp_len, size_max; |
| 1027 | unsigned char *addr; |
| 1028 | int len_sum = 0; |
| 1029 | int status = 0; |
| 1030 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1031 | mutex_lock(&po->pg_vec_lock); |
| 1032 | |
| 1033 | err = -EBUSY; |
| 1034 | if (saddr == NULL) { |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1035 | dev = po->prot_hook.dev; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1036 | proto = po->num; |
| 1037 | addr = NULL; |
| 1038 | } else { |
| 1039 | err = -EINVAL; |
| 1040 | if (msg->msg_namelen < sizeof(struct sockaddr_ll)) |
| 1041 | goto out; |
| 1042 | if (msg->msg_namelen < (saddr->sll_halen |
| 1043 | + offsetof(struct sockaddr_ll, |
| 1044 | sll_addr))) |
| 1045 | goto out; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1046 | proto = saddr->sll_protocol; |
| 1047 | addr = saddr->sll_addr; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1048 | dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex); |
| 1049 | need_rls_dev = true; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1052 | err = -ENXIO; |
| 1053 | if (unlikely(dev == NULL)) |
| 1054 | goto out; |
| 1055 | |
| 1056 | reserve = dev->hard_header_len; |
| 1057 | |
| 1058 | err = -ENETDOWN; |
| 1059 | if (unlikely(!(dev->flags & IFF_UP))) |
| 1060 | goto out_put; |
| 1061 | |
| 1062 | size_max = po->tx_ring.frame_size |
Gabor Gombas | b5dd884 | 2009-10-29 03:19:11 -0700 | [diff] [blame] | 1063 | - (po->tp_hdrlen - sizeof(struct sockaddr_ll)); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1064 | |
| 1065 | if (size_max > dev->mtu + reserve) |
| 1066 | size_max = dev->mtu + reserve; |
| 1067 | |
| 1068 | do { |
| 1069 | ph = packet_current_frame(po, &po->tx_ring, |
| 1070 | TP_STATUS_SEND_REQUEST); |
| 1071 | |
| 1072 | if (unlikely(ph == NULL)) { |
| 1073 | schedule(); |
| 1074 | continue; |
| 1075 | } |
| 1076 | |
| 1077 | status = TP_STATUS_SEND_REQUEST; |
| 1078 | skb = sock_alloc_send_skb(&po->sk, |
| 1079 | LL_ALLOCATED_SPACE(dev) |
| 1080 | + sizeof(struct sockaddr_ll), |
| 1081 | 0, &err); |
| 1082 | |
| 1083 | if (unlikely(skb == NULL)) |
| 1084 | goto out_status; |
| 1085 | |
| 1086 | tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto, |
| 1087 | addr); |
| 1088 | |
| 1089 | if (unlikely(tp_len < 0)) { |
| 1090 | if (po->tp_loss) { |
| 1091 | __packet_set_status(po, ph, |
| 1092 | TP_STATUS_AVAILABLE); |
| 1093 | packet_increment_head(&po->tx_ring); |
| 1094 | kfree_skb(skb); |
| 1095 | continue; |
| 1096 | } else { |
| 1097 | status = TP_STATUS_WRONG_FORMAT; |
| 1098 | err = tp_len; |
| 1099 | goto out_status; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | skb->destructor = tpacket_destruct_skb; |
| 1104 | __packet_set_status(po, ph, TP_STATUS_SENDING); |
| 1105 | atomic_inc(&po->tx_ring.pending); |
| 1106 | |
| 1107 | status = TP_STATUS_SEND_REQUEST; |
| 1108 | err = dev_queue_xmit(skb); |
Jarek Poplawski | eb70df1 | 2010-01-10 22:04:19 +0000 | [diff] [blame] | 1109 | if (unlikely(err > 0)) { |
| 1110 | err = net_xmit_errno(err); |
| 1111 | if (err && __packet_get_status(po, ph) == |
| 1112 | TP_STATUS_AVAILABLE) { |
| 1113 | /* skb was destructed already */ |
| 1114 | skb = NULL; |
| 1115 | goto out_status; |
| 1116 | } |
| 1117 | /* |
| 1118 | * skb was dropped but not destructed yet; |
| 1119 | * let's treat it like congestion or err < 0 |
| 1120 | */ |
| 1121 | err = 0; |
| 1122 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1123 | packet_increment_head(&po->tx_ring); |
| 1124 | len_sum += tp_len; |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1125 | } while (likely((ph != NULL) || |
| 1126 | ((!(msg->msg_flags & MSG_DONTWAIT)) && |
| 1127 | (atomic_read(&po->tx_ring.pending)))) |
| 1128 | ); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1129 | |
| 1130 | err = len_sum; |
| 1131 | goto out_put; |
| 1132 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1133 | out_status: |
| 1134 | __packet_set_status(po, ph, status); |
| 1135 | kfree_skb(skb); |
| 1136 | out_put: |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1137 | if (need_rls_dev) |
| 1138 | dev_put(dev); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1139 | out: |
| 1140 | mutex_unlock(&po->pg_vec_lock); |
| 1141 | return err; |
| 1142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1144 | static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad, |
| 1145 | size_t reserve, size_t len, |
| 1146 | size_t linear, int noblock, |
| 1147 | int *err) |
| 1148 | { |
| 1149 | struct sk_buff *skb; |
| 1150 | |
| 1151 | /* Under a page? Don't bother with paged skb. */ |
| 1152 | if (prepad + len < PAGE_SIZE || !linear) |
| 1153 | linear = len; |
| 1154 | |
| 1155 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
| 1156 | err); |
| 1157 | if (!skb) |
| 1158 | return NULL; |
| 1159 | |
| 1160 | skb_reserve(skb, reserve); |
| 1161 | skb_put(skb, linear); |
| 1162 | skb->data_len = len - linear; |
| 1163 | skb->len += len - linear; |
| 1164 | |
| 1165 | return skb; |
| 1166 | } |
| 1167 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1168 | static int packet_snd(struct socket *sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | struct msghdr *msg, size_t len) |
| 1170 | { |
| 1171 | struct sock *sk = sock->sk; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1172 | struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | struct sk_buff *skb; |
| 1174 | struct net_device *dev; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1175 | __be16 proto; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1176 | bool need_rls_dev = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | unsigned char *addr; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1178 | int err, reserve = 0; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1179 | struct virtio_net_hdr vnet_hdr = { 0 }; |
| 1180 | int offset = 0; |
| 1181 | int vnet_hdr_len; |
| 1182 | struct packet_sock *po = pkt_sk(sk); |
| 1183 | unsigned short gso_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | |
| 1185 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1186 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | if (saddr == NULL) { |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1190 | dev = po->prot_hook.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | proto = po->num; |
| 1192 | addr = NULL; |
| 1193 | } else { |
| 1194 | err = -EINVAL; |
| 1195 | if (msg->msg_namelen < sizeof(struct sockaddr_ll)) |
| 1196 | goto out; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1197 | if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) |
| 1198 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | proto = saddr->sll_protocol; |
| 1200 | addr = saddr->sll_addr; |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1201 | dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex); |
| 1202 | need_rls_dev = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | err = -ENXIO; |
| 1206 | if (dev == NULL) |
| 1207 | goto out_unlock; |
| 1208 | if (sock->type == SOCK_RAW) |
| 1209 | reserve = dev->hard_header_len; |
| 1210 | |
David S. Miller | d5e76b0 | 2007-01-25 19:30:36 -0800 | [diff] [blame] | 1211 | err = -ENETDOWN; |
| 1212 | if (!(dev->flags & IFF_UP)) |
| 1213 | goto out_unlock; |
| 1214 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1215 | if (po->has_vnet_hdr) { |
| 1216 | vnet_hdr_len = sizeof(vnet_hdr); |
| 1217 | |
| 1218 | err = -EINVAL; |
| 1219 | if (len < vnet_hdr_len) |
| 1220 | goto out_unlock; |
| 1221 | |
| 1222 | len -= vnet_hdr_len; |
| 1223 | |
| 1224 | err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov, |
| 1225 | vnet_hdr_len); |
| 1226 | if (err < 0) |
| 1227 | goto out_unlock; |
| 1228 | |
| 1229 | if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
| 1230 | (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 > |
| 1231 | vnet_hdr.hdr_len)) |
| 1232 | vnet_hdr.hdr_len = vnet_hdr.csum_start + |
| 1233 | vnet_hdr.csum_offset + 2; |
| 1234 | |
| 1235 | err = -EINVAL; |
| 1236 | if (vnet_hdr.hdr_len > len) |
| 1237 | goto out_unlock; |
| 1238 | |
| 1239 | if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 1240 | switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
| 1241 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 1242 | gso_type = SKB_GSO_TCPV4; |
| 1243 | break; |
| 1244 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 1245 | gso_type = SKB_GSO_TCPV6; |
| 1246 | break; |
| 1247 | case VIRTIO_NET_HDR_GSO_UDP: |
| 1248 | gso_type = SKB_GSO_UDP; |
| 1249 | break; |
| 1250 | default: |
| 1251 | goto out_unlock; |
| 1252 | } |
| 1253 | |
| 1254 | if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) |
| 1255 | gso_type |= SKB_GSO_TCP_ECN; |
| 1256 | |
| 1257 | if (vnet_hdr.gso_size == 0) |
| 1258 | goto out_unlock; |
| 1259 | |
| 1260 | } |
| 1261 | } |
| 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | err = -EMSGSIZE; |
Ben Greear | 57f89bf | 2011-02-11 09:35:18 +0000 | [diff] [blame] | 1264 | if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | goto out_unlock; |
| 1266 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1267 | err = -ENOBUFS; |
| 1268 | skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev), |
| 1269 | LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len, |
| 1270 | msg->msg_flags & MSG_DONTWAIT, &err); |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1271 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | goto out_unlock; |
| 1273 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1274 | skb_set_network_header(skb, reserve); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 1276 | err = -EINVAL; |
| 1277 | if (sock->type == SOCK_DGRAM && |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1278 | (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0) |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 1279 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
| 1281 | /* Returns -EFAULT on error */ |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1282 | err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | if (err) |
| 1284 | goto out_free; |
Oliver Hartkopp | 2244d07 | 2010-08-17 08:59:14 +0000 | [diff] [blame] | 1285 | err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags); |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 1286 | if (err < 0) |
| 1287 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
Ben Greear | 57f89bf | 2011-02-11 09:35:18 +0000 | [diff] [blame] | 1289 | if (!gso_type && (len > dev->mtu + reserve)) { |
| 1290 | /* Earlier code assumed this would be a VLAN pkt, |
| 1291 | * double-check this now that we have the actual |
| 1292 | * packet in hand. |
| 1293 | */ |
| 1294 | struct ethhdr *ehdr; |
| 1295 | skb_reset_mac_header(skb); |
| 1296 | ehdr = eth_hdr(skb); |
| 1297 | if (ehdr->h_proto != htons(ETH_P_8021Q)) { |
| 1298 | err = -EMSGSIZE; |
| 1299 | goto out_free; |
| 1300 | } |
| 1301 | } |
| 1302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | skb->protocol = proto; |
| 1304 | skb->dev = dev; |
| 1305 | skb->priority = sk->sk_priority; |
Eric Dumazet | 2d37a18 | 2009-10-01 19:14:46 +0000 | [diff] [blame] | 1306 | skb->mark = sk->sk_mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1308 | if (po->has_vnet_hdr) { |
| 1309 | if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 1310 | if (!skb_partial_csum_set(skb, vnet_hdr.csum_start, |
| 1311 | vnet_hdr.csum_offset)) { |
| 1312 | err = -EINVAL; |
| 1313 | goto out_free; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | skb_shinfo(skb)->gso_size = vnet_hdr.gso_size; |
| 1318 | skb_shinfo(skb)->gso_type = gso_type; |
| 1319 | |
| 1320 | /* Header must be checked, and gso_segs computed. */ |
| 1321 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 1322 | skb_shinfo(skb)->gso_segs = 0; |
| 1323 | |
| 1324 | len += vnet_hdr_len; |
| 1325 | } |
| 1326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * Now send it |
| 1329 | */ |
| 1330 | |
| 1331 | err = dev_queue_xmit(skb); |
| 1332 | if (err > 0 && (err = net_xmit_errno(err)) != 0) |
| 1333 | goto out_unlock; |
| 1334 | |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1335 | if (need_rls_dev) |
| 1336 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1338 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
| 1340 | out_free: |
| 1341 | kfree_skb(skb); |
| 1342 | out_unlock: |
Ben Greear | 827d978 | 2011-06-01 07:18:53 +0000 | [diff] [blame] | 1343 | if (dev && need_rls_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | dev_put(dev); |
| 1345 | out: |
| 1346 | return err; |
| 1347 | } |
| 1348 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1349 | static int packet_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 1350 | struct msghdr *msg, size_t len) |
| 1351 | { |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1352 | struct sock *sk = sock->sk; |
| 1353 | struct packet_sock *po = pkt_sk(sk); |
| 1354 | if (po->tx_ring.pg_vec) |
| 1355 | return tpacket_snd(po, msg); |
| 1356 | else |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1357 | return packet_snd(sock, msg, len); |
| 1358 | } |
| 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | /* |
| 1361 | * Close a PACKET socket. This is fairly simple. We immediately go |
| 1362 | * to 'closed' state and remove our protocol entry in the device list. |
| 1363 | */ |
| 1364 | |
| 1365 | static int packet_release(struct socket *sock) |
| 1366 | { |
| 1367 | struct sock *sk = sock->sk; |
| 1368 | struct packet_sock *po; |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 1369 | struct net *net; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1370 | struct tpacket_req req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
| 1372 | if (!sk) |
| 1373 | return 0; |
| 1374 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1375 | net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | po = pkt_sk(sk); |
| 1377 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 1378 | spin_lock_bh(&net->packet.sklist_lock); |
| 1379 | sk_del_node_init_rcu(sk); |
Eric Dumazet | 920de80 | 2008-11-24 00:09:29 -0800 | [diff] [blame] | 1380 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 1381 | spin_unlock_bh(&net->packet.sklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 1383 | spin_lock(&po->bind_lock); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 1384 | unregister_prot_hook(sk, false); |
Ben Greear | 160ff18 | 2011-06-01 07:18:52 +0000 | [diff] [blame] | 1385 | if (po->prot_hook.dev) { |
| 1386 | dev_put(po->prot_hook.dev); |
| 1387 | po->prot_hook.dev = NULL; |
| 1388 | } |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 1389 | spin_unlock(&po->bind_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | packet_flush_mclist(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1393 | memset(&req, 0, sizeof(req)); |
| 1394 | |
| 1395 | if (po->rx_ring.pg_vec) |
| 1396 | packet_set_ring(sk, &req, 1, 0); |
| 1397 | |
| 1398 | if (po->tx_ring.pg_vec) |
| 1399 | packet_set_ring(sk, &req, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 1401 | synchronize_net(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | /* |
| 1403 | * Now the socket is dead. No more input will appear. |
| 1404 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | sock_orphan(sk); |
| 1406 | sock->sk = NULL; |
| 1407 | |
| 1408 | /* Purge queues */ |
| 1409 | |
| 1410 | skb_queue_purge(&sk->sk_receive_queue); |
Pavel Emelyanov | 17ab56a | 2007-11-10 21:38:48 -0800 | [diff] [blame] | 1411 | sk_refcnt_debug_release(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
| 1413 | sock_put(sk); |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * Attach a packet hook. |
| 1419 | */ |
| 1420 | |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1421 | static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | { |
| 1423 | struct packet_sock *po = pkt_sk(sk); |
| 1424 | /* |
| 1425 | * Detach an existing hook if present. |
| 1426 | */ |
| 1427 | |
| 1428 | lock_sock(sk); |
| 1429 | |
| 1430 | spin_lock(&po->bind_lock); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 1431 | unregister_prot_hook(sk, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | po->num = protocol; |
| 1433 | po->prot_hook.type = protocol; |
Ben Greear | 160ff18 | 2011-06-01 07:18:52 +0000 | [diff] [blame] | 1434 | if (po->prot_hook.dev) |
| 1435 | dev_put(po->prot_hook.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | po->prot_hook.dev = dev; |
| 1437 | |
| 1438 | po->ifindex = dev ? dev->ifindex : 0; |
| 1439 | |
| 1440 | if (protocol == 0) |
| 1441 | goto out_unlock; |
| 1442 | |
Urs Thuermann | be85d4a | 2007-11-12 21:05:20 -0800 | [diff] [blame] | 1443 | if (!dev || (dev->flags & IFF_UP)) { |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 1444 | register_prot_hook(sk); |
Urs Thuermann | be85d4a | 2007-11-12 21:05:20 -0800 | [diff] [blame] | 1445 | } else { |
| 1446 | sk->sk_err = ENETDOWN; |
| 1447 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1448 | sk->sk_error_report(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | out_unlock: |
| 1452 | spin_unlock(&po->bind_lock); |
| 1453 | release_sock(sk); |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Bind a packet socket to a device |
| 1459 | */ |
| 1460 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1461 | static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, |
| 1462 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1464 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | char name[15]; |
| 1466 | struct net_device *dev; |
| 1467 | int err = -ENODEV; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | /* |
| 1470 | * Check legality |
| 1471 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1472 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 1473 | if (addr_len != sizeof(struct sockaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | return -EINVAL; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1475 | strlcpy(name, uaddr->sa_data, sizeof(name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1477 | dev = dev_get_by_name(sock_net(sk), name); |
Ben Greear | 160ff18 | 2011-06-01 07:18:52 +0000 | [diff] [blame] | 1478 | if (dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | err = packet_do_bind(sk, dev, pkt_sk(sk)->num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | return err; |
| 1481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
| 1483 | static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 1484 | { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1485 | struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr; |
| 1486 | struct sock *sk = sock->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | struct net_device *dev = NULL; |
| 1488 | int err; |
| 1489 | |
| 1490 | |
| 1491 | /* |
| 1492 | * Check legality |
| 1493 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | if (addr_len < sizeof(struct sockaddr_ll)) |
| 1496 | return -EINVAL; |
| 1497 | if (sll->sll_family != AF_PACKET) |
| 1498 | return -EINVAL; |
| 1499 | |
| 1500 | if (sll->sll_ifindex) { |
| 1501 | err = -ENODEV; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1502 | dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | if (dev == NULL) |
| 1504 | goto out; |
| 1505 | } |
| 1506 | err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
| 1508 | out: |
| 1509 | return err; |
| 1510 | } |
| 1511 | |
| 1512 | static struct proto packet_proto = { |
| 1513 | .name = "PACKET", |
| 1514 | .owner = THIS_MODULE, |
| 1515 | .obj_size = sizeof(struct packet_sock), |
| 1516 | }; |
| 1517 | |
| 1518 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1519 | * Create a packet of type SOCK_PACKET. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | */ |
| 1521 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1522 | static int packet_create(struct net *net, struct socket *sock, int protocol, |
| 1523 | int kern) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | { |
| 1525 | struct sock *sk; |
| 1526 | struct packet_sock *po; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1527 | __be16 proto = (__force __be16)protocol; /* weird, but documented */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | int err; |
| 1529 | |
| 1530 | if (!capable(CAP_NET_RAW)) |
| 1531 | return -EPERM; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 1532 | if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW && |
| 1533 | sock->type != SOCK_PACKET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | return -ESOCKTNOSUPPORT; |
| 1535 | |
| 1536 | sock->state = SS_UNCONNECTED; |
| 1537 | |
| 1538 | err = -ENOBUFS; |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 1539 | sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | if (sk == NULL) |
| 1541 | goto out; |
| 1542 | |
| 1543 | sock->ops = &packet_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | if (sock->type == SOCK_PACKET) |
| 1545 | sock->ops = &packet_ops_spkt; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | sock_init_data(sock, sk); |
| 1548 | |
| 1549 | po = pkt_sk(sk); |
| 1550 | sk->sk_family = PF_PACKET; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1551 | po->num = proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | |
| 1553 | sk->sk_destruct = packet_sock_destruct; |
Pavel Emelyanov | 17ab56a | 2007-11-10 21:38:48 -0800 | [diff] [blame] | 1554 | sk_refcnt_debug_inc(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
| 1556 | /* |
| 1557 | * Attach a protocol block |
| 1558 | */ |
| 1559 | |
| 1560 | spin_lock_init(&po->bind_lock); |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 1561 | mutex_init(&po->pg_vec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | po->prot_hook.func = packet_rcv; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | if (sock->type == SOCK_PACKET) |
| 1565 | po->prot_hook.func = packet_rcv_spkt; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 1566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | po->prot_hook.af_packet_priv = sk; |
| 1568 | |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1569 | if (proto) { |
| 1570 | po->prot_hook.type = proto; |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 1571 | register_prot_hook(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 1574 | spin_lock_bh(&net->packet.sklist_lock); |
| 1575 | sk_add_node_rcu(sk, &net->packet.sklist); |
Eric Dumazet | 3680453 | 2008-11-19 14:25:35 -0800 | [diff] [blame] | 1576 | sock_prot_inuse_add(net, &packet_proto, 1); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 1577 | spin_unlock_bh(&net->packet.sklist_lock); |
| 1578 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1579 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | out: |
| 1581 | return err; |
| 1582 | } |
| 1583 | |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 1584 | static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len) |
| 1585 | { |
| 1586 | struct sock_exterr_skb *serr; |
| 1587 | struct sk_buff *skb, *skb2; |
| 1588 | int copied, err; |
| 1589 | |
| 1590 | err = -EAGAIN; |
| 1591 | skb = skb_dequeue(&sk->sk_error_queue); |
| 1592 | if (skb == NULL) |
| 1593 | goto out; |
| 1594 | |
| 1595 | copied = skb->len; |
| 1596 | if (copied > len) { |
| 1597 | msg->msg_flags |= MSG_TRUNC; |
| 1598 | copied = len; |
| 1599 | } |
| 1600 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1601 | if (err) |
| 1602 | goto out_free_skb; |
| 1603 | |
| 1604 | sock_recv_timestamp(msg, sk, skb); |
| 1605 | |
| 1606 | serr = SKB_EXT_ERR(skb); |
| 1607 | put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP, |
| 1608 | sizeof(serr->ee), &serr->ee); |
| 1609 | |
| 1610 | msg->msg_flags |= MSG_ERRQUEUE; |
| 1611 | err = copied; |
| 1612 | |
| 1613 | /* Reset and regenerate socket error */ |
| 1614 | spin_lock_bh(&sk->sk_error_queue.lock); |
| 1615 | sk->sk_err = 0; |
| 1616 | if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) { |
| 1617 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; |
| 1618 | spin_unlock_bh(&sk->sk_error_queue.lock); |
| 1619 | sk->sk_error_report(sk); |
| 1620 | } else |
| 1621 | spin_unlock_bh(&sk->sk_error_queue.lock); |
| 1622 | |
| 1623 | out_free_skb: |
| 1624 | kfree_skb(skb); |
| 1625 | out: |
| 1626 | return err; |
| 1627 | } |
| 1628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | /* |
| 1630 | * Pull a packet from our receive queue and hand it to the user. |
| 1631 | * If necessary we block. |
| 1632 | */ |
| 1633 | |
| 1634 | static int packet_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1635 | struct msghdr *msg, size_t len, int flags) |
| 1636 | { |
| 1637 | struct sock *sk = sock->sk; |
| 1638 | struct sk_buff *skb; |
| 1639 | int copied, err; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1640 | struct sockaddr_ll *sll; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1641 | int vnet_hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
| 1643 | err = -EINVAL; |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 1644 | if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | goto out; |
| 1646 | |
| 1647 | #if 0 |
| 1648 | /* What error should we return now? EUNATTACH? */ |
| 1649 | if (pkt_sk(sk)->ifindex < 0) |
| 1650 | return -ENODEV; |
| 1651 | #endif |
| 1652 | |
Richard Cochran | ed85b56 | 2010-04-07 22:41:28 +0000 | [diff] [blame] | 1653 | if (flags & MSG_ERRQUEUE) { |
| 1654 | err = packet_recv_error(sk, msg, len); |
| 1655 | goto out; |
| 1656 | } |
| 1657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | * Call the generic datagram receiver. This handles all sorts |
| 1660 | * of horrible races and re-entrancy so we can forget about it |
| 1661 | * in the protocol layers. |
| 1662 | * |
| 1663 | * Now it will return ENETDOWN, if device have just gone down, |
| 1664 | * but then it will block. |
| 1665 | */ |
| 1666 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1667 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | |
| 1669 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1670 | * An error occurred so return it. Because skb_recv_datagram() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | * handles the blocking we don't see and worry about blocking |
| 1672 | * retries. |
| 1673 | */ |
| 1674 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 1675 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | goto out; |
| 1677 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1678 | if (pkt_sk(sk)->has_vnet_hdr) { |
| 1679 | struct virtio_net_hdr vnet_hdr = { 0 }; |
| 1680 | |
| 1681 | err = -EINVAL; |
| 1682 | vnet_hdr_len = sizeof(vnet_hdr); |
Mariusz Kozlowski | 1f18b71 | 2010-11-08 11:58:45 +0000 | [diff] [blame] | 1683 | if (len < vnet_hdr_len) |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1684 | goto out_free; |
| 1685 | |
Mariusz Kozlowski | 1f18b71 | 2010-11-08 11:58:45 +0000 | [diff] [blame] | 1686 | len -= vnet_hdr_len; |
| 1687 | |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1688 | if (skb_is_gso(skb)) { |
| 1689 | struct skb_shared_info *sinfo = skb_shinfo(skb); |
| 1690 | |
| 1691 | /* This is a hint as to how much should be linear. */ |
| 1692 | vnet_hdr.hdr_len = skb_headlen(skb); |
| 1693 | vnet_hdr.gso_size = sinfo->gso_size; |
| 1694 | if (sinfo->gso_type & SKB_GSO_TCPV4) |
| 1695 | vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 1696 | else if (sinfo->gso_type & SKB_GSO_TCPV6) |
| 1697 | vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
| 1698 | else if (sinfo->gso_type & SKB_GSO_UDP) |
| 1699 | vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP; |
| 1700 | else if (sinfo->gso_type & SKB_GSO_FCOE) |
| 1701 | goto out_free; |
| 1702 | else |
| 1703 | BUG(); |
| 1704 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) |
| 1705 | vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
| 1706 | } else |
| 1707 | vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; |
| 1708 | |
| 1709 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1710 | vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 1711 | vnet_hdr.csum_start = skb_checksum_start_offset(skb); |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1712 | vnet_hdr.csum_offset = skb->csum_offset; |
Jason Wang | 10a8d94 | 2011-06-10 00:56:17 +0000 | [diff] [blame] | 1713 | } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { |
| 1714 | vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1715 | } /* else everything is zero */ |
| 1716 | |
| 1717 | err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr, |
| 1718 | vnet_hdr_len); |
| 1719 | if (err < 0) |
| 1720 | goto out_free; |
| 1721 | } |
| 1722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | /* |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1724 | * If the address length field is there to be filled in, we fill |
| 1725 | * it in now. |
| 1726 | */ |
| 1727 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1728 | sll = &PACKET_SKB_CB(skb)->sa.ll; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1729 | if (sock->type == SOCK_PACKET) |
| 1730 | msg->msg_namelen = sizeof(struct sockaddr_pkt); |
| 1731 | else |
| 1732 | msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr); |
| 1733 | |
| 1734 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | * You lose any data beyond the buffer you gave. If it worries a |
| 1736 | * user program they can ask the device for its MTU anyway. |
| 1737 | */ |
| 1738 | |
| 1739 | copied = skb->len; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1740 | if (copied > len) { |
| 1741 | copied = len; |
| 1742 | msg->msg_flags |= MSG_TRUNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1746 | if (err) |
| 1747 | goto out_free; |
| 1748 | |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 1749 | sock_recv_ts_and_drops(msg, sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
| 1751 | if (msg->msg_name) |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1752 | memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, |
| 1753 | msg->msg_namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1755 | if (pkt_sk(sk)->auxdata) { |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1756 | struct tpacket_auxdata aux; |
| 1757 | |
| 1758 | aux.tp_status = TP_STATUS_USER; |
| 1759 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1760 | aux.tp_status |= TP_STATUS_CSUMNOTREADY; |
| 1761 | aux.tp_len = PACKET_SKB_CB(skb)->origlen; |
| 1762 | aux.tp_snaplen = skb->len; |
| 1763 | aux.tp_mac = 0; |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 1764 | aux.tp_net = skb_network_offset(skb); |
Ben Greear | a3bcc23 | 2011-06-01 06:49:10 +0000 | [diff] [blame] | 1765 | if (vlan_tx_tag_present(skb)) { |
| 1766 | aux.tp_vlan_tci = vlan_tx_tag_get(skb); |
| 1767 | aux.tp_status |= TP_STATUS_VLAN_VALID; |
| 1768 | } else { |
| 1769 | aux.tp_vlan_tci = 0; |
| 1770 | } |
Eric Dumazet | 13fcb7b | 2011-06-06 22:42:06 -0700 | [diff] [blame] | 1771 | aux.tp_padding = 0; |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1772 | put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux); |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | /* |
| 1776 | * Free or return the buffer as appropriate. Again this |
| 1777 | * hides all the races and re-entrancy issues from us. |
| 1778 | */ |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 1779 | err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
| 1781 | out_free: |
| 1782 | skb_free_datagram(sk, skb); |
| 1783 | out: |
| 1784 | return err; |
| 1785 | } |
| 1786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr, |
| 1788 | int *uaddr_len, int peer) |
| 1789 | { |
| 1790 | struct net_device *dev; |
| 1791 | struct sock *sk = sock->sk; |
| 1792 | |
| 1793 | if (peer) |
| 1794 | return -EOPNOTSUPP; |
| 1795 | |
| 1796 | uaddr->sa_family = AF_PACKET; |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 1797 | rcu_read_lock(); |
| 1798 | dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex); |
| 1799 | if (dev) |
Vasiliy Kulikov | 6728664 | 2010-11-10 12:09:10 -0800 | [diff] [blame] | 1800 | strncpy(uaddr->sa_data, dev->name, 14); |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 1801 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | memset(uaddr->sa_data, 0, 14); |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 1803 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | *uaddr_len = sizeof(*uaddr); |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | |
| 1809 | static int packet_getname(struct socket *sock, struct sockaddr *uaddr, |
| 1810 | int *uaddr_len, int peer) |
| 1811 | { |
| 1812 | struct net_device *dev; |
| 1813 | struct sock *sk = sock->sk; |
| 1814 | struct packet_sock *po = pkt_sk(sk); |
Cyrill Gorcunov | 13cfa97 | 2009-11-08 05:51:19 +0000 | [diff] [blame] | 1815 | DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | |
| 1817 | if (peer) |
| 1818 | return -EOPNOTSUPP; |
| 1819 | |
| 1820 | sll->sll_family = AF_PACKET; |
| 1821 | sll->sll_ifindex = po->ifindex; |
| 1822 | sll->sll_protocol = po->num; |
Vasiliy Kulikov | 6728664 | 2010-11-10 12:09:10 -0800 | [diff] [blame] | 1823 | sll->sll_pkttype = 0; |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 1824 | rcu_read_lock(); |
| 1825 | dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | if (dev) { |
| 1827 | sll->sll_hatype = dev->type; |
| 1828 | sll->sll_halen = dev->addr_len; |
| 1829 | memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | } else { |
| 1831 | sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */ |
| 1832 | sll->sll_halen = 0; |
| 1833 | } |
Eric Dumazet | 654d1f8 | 2009-11-02 10:43:32 +0100 | [diff] [blame] | 1834 | rcu_read_unlock(); |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1835 | *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 1840 | static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i, |
| 1841 | int what) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | { |
| 1843 | switch (i->type) { |
| 1844 | case PACKET_MR_MULTICAST: |
Jiri Pirko | 1162563 | 2010-03-02 20:40:01 +0000 | [diff] [blame] | 1845 | if (i->alen != dev->addr_len) |
| 1846 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | if (what > 0) |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1848 | return dev_mc_add(dev, i->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | else |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 1850 | return dev_mc_del(dev, i->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | break; |
| 1852 | case PACKET_MR_PROMISC: |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 1853 | return dev_set_promiscuity(dev, what); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | break; |
| 1855 | case PACKET_MR_ALLMULTI: |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 1856 | return dev_set_allmulti(dev, what); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | break; |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 1858 | case PACKET_MR_UNICAST: |
Jiri Pirko | 1162563 | 2010-03-02 20:40:01 +0000 | [diff] [blame] | 1859 | if (i->alen != dev->addr_len) |
| 1860 | return -EINVAL; |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 1861 | if (what > 0) |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 1862 | return dev_uc_add(dev, i->addr); |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 1863 | else |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 1864 | return dev_uc_del(dev, i->addr); |
Eric W. Biederman | d95ed92 | 2009-05-19 18:27:17 +0000 | [diff] [blame] | 1865 | break; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1866 | default: |
| 1867 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | } |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 1869 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what) |
| 1873 | { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 1874 | for ( ; i; i = i->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | if (i->ifindex == dev->ifindex) |
| 1876 | packet_dev_mc(dev, i, what); |
| 1877 | } |
| 1878 | } |
| 1879 | |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1880 | static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | { |
| 1882 | struct packet_sock *po = pkt_sk(sk); |
| 1883 | struct packet_mclist *ml, *i; |
| 1884 | struct net_device *dev; |
| 1885 | int err; |
| 1886 | |
| 1887 | rtnl_lock(); |
| 1888 | |
| 1889 | err = -ENODEV; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1890 | dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | if (!dev) |
| 1892 | goto done; |
| 1893 | |
| 1894 | err = -EINVAL; |
Jiri Pirko | 1162563 | 2010-03-02 20:40:01 +0000 | [diff] [blame] | 1895 | if (mreq->mr_alen > dev->addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | goto done; |
| 1897 | |
| 1898 | err = -ENOBUFS; |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 1899 | i = kmalloc(sizeof(*i), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | if (i == NULL) |
| 1901 | goto done; |
| 1902 | |
| 1903 | err = 0; |
| 1904 | for (ml = po->mclist; ml; ml = ml->next) { |
| 1905 | if (ml->ifindex == mreq->mr_ifindex && |
| 1906 | ml->type == mreq->mr_type && |
| 1907 | ml->alen == mreq->mr_alen && |
| 1908 | memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { |
| 1909 | ml->count++; |
| 1910 | /* Free the new element ... */ |
| 1911 | kfree(i); |
| 1912 | goto done; |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | i->type = mreq->mr_type; |
| 1917 | i->ifindex = mreq->mr_ifindex; |
| 1918 | i->alen = mreq->mr_alen; |
| 1919 | memcpy(i->addr, mreq->mr_address, i->alen); |
| 1920 | i->count = 1; |
| 1921 | i->next = po->mclist; |
| 1922 | po->mclist = i; |
Wang Chen | 2aeb0b8 | 2008-07-14 20:49:46 -0700 | [diff] [blame] | 1923 | err = packet_dev_mc(dev, i, 1); |
| 1924 | if (err) { |
| 1925 | po->mclist = i->next; |
| 1926 | kfree(i); |
| 1927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | |
| 1929 | done: |
| 1930 | rtnl_unlock(); |
| 1931 | return err; |
| 1932 | } |
| 1933 | |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1934 | static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | { |
| 1936 | struct packet_mclist *ml, **mlp; |
| 1937 | |
| 1938 | rtnl_lock(); |
| 1939 | |
| 1940 | for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) { |
| 1941 | if (ml->ifindex == mreq->mr_ifindex && |
| 1942 | ml->type == mreq->mr_type && |
| 1943 | ml->alen == mreq->mr_alen && |
| 1944 | memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { |
| 1945 | if (--ml->count == 0) { |
| 1946 | struct net_device *dev; |
| 1947 | *mlp = ml->next; |
Eric Dumazet | ad959e7 | 2009-10-16 06:38:46 +0000 | [diff] [blame] | 1948 | dev = __dev_get_by_index(sock_net(sk), ml->ifindex); |
| 1949 | if (dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | packet_dev_mc(dev, ml, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | kfree(ml); |
| 1952 | } |
| 1953 | rtnl_unlock(); |
| 1954 | return 0; |
| 1955 | } |
| 1956 | } |
| 1957 | rtnl_unlock(); |
| 1958 | return -EADDRNOTAVAIL; |
| 1959 | } |
| 1960 | |
| 1961 | static void packet_flush_mclist(struct sock *sk) |
| 1962 | { |
| 1963 | struct packet_sock *po = pkt_sk(sk); |
| 1964 | struct packet_mclist *ml; |
| 1965 | |
| 1966 | if (!po->mclist) |
| 1967 | return; |
| 1968 | |
| 1969 | rtnl_lock(); |
| 1970 | while ((ml = po->mclist) != NULL) { |
| 1971 | struct net_device *dev; |
| 1972 | |
| 1973 | po->mclist = ml->next; |
Eric Dumazet | ad959e7 | 2009-10-16 06:38:46 +0000 | [diff] [blame] | 1974 | dev = __dev_get_by_index(sock_net(sk), ml->ifindex); |
| 1975 | if (dev != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | packet_dev_mc(dev, ml, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | kfree(ml); |
| 1978 | } |
| 1979 | rtnl_unlock(); |
| 1980 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
| 1982 | static int |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1983 | packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | { |
| 1985 | struct sock *sk = sock->sk; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1986 | struct packet_sock *po = pkt_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | int ret; |
| 1988 | |
| 1989 | if (level != SOL_PACKET) |
| 1990 | return -ENOPROTOOPT; |
| 1991 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 1992 | switch (optname) { |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1993 | case PACKET_ADD_MEMBERSHIP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | case PACKET_DROP_MEMBERSHIP: |
| 1995 | { |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1996 | struct packet_mreq_max mreq; |
| 1997 | int len = optlen; |
| 1998 | memset(&mreq, 0, sizeof(mreq)); |
| 1999 | if (len < sizeof(struct packet_mreq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | return -EINVAL; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 2001 | if (len > sizeof(mreq)) |
| 2002 | len = sizeof(mreq); |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2003 | if (copy_from_user(&mreq, optval, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | return -EFAULT; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 2005 | if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address))) |
| 2006 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | if (optname == PACKET_ADD_MEMBERSHIP) |
| 2008 | ret = packet_mc_add(sk, &mreq); |
| 2009 | else |
| 2010 | ret = packet_mc_drop(sk, &mreq); |
| 2011 | return ret; |
| 2012 | } |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 2013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | case PACKET_RX_RING: |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2015 | case PACKET_TX_RING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | { |
| 2017 | struct tpacket_req req; |
| 2018 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2019 | if (optlen < sizeof(req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | return -EINVAL; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2021 | if (pkt_sk(sk)->has_vnet_hdr) |
| 2022 | return -EINVAL; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2023 | if (copy_from_user(&req, optval, sizeof(req))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | return -EFAULT; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2025 | return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | } |
| 2027 | case PACKET_COPY_THRESH: |
| 2028 | { |
| 2029 | int val; |
| 2030 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2031 | if (optlen != sizeof(val)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | return -EINVAL; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2033 | if (copy_from_user(&val, optval, sizeof(val))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | return -EFAULT; |
| 2035 | |
| 2036 | pkt_sk(sk)->copy_thresh = val; |
| 2037 | return 0; |
| 2038 | } |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2039 | case PACKET_VERSION: |
| 2040 | { |
| 2041 | int val; |
| 2042 | |
| 2043 | if (optlen != sizeof(val)) |
| 2044 | return -EINVAL; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2045 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2046 | return -EBUSY; |
| 2047 | if (copy_from_user(&val, optval, sizeof(val))) |
| 2048 | return -EFAULT; |
| 2049 | switch (val) { |
| 2050 | case TPACKET_V1: |
| 2051 | case TPACKET_V2: |
| 2052 | po->tp_version = val; |
| 2053 | return 0; |
| 2054 | default: |
| 2055 | return -EINVAL; |
| 2056 | } |
| 2057 | } |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 2058 | case PACKET_RESERVE: |
| 2059 | { |
| 2060 | unsigned int val; |
| 2061 | |
| 2062 | if (optlen != sizeof(val)) |
| 2063 | return -EINVAL; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2064 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 2065 | return -EBUSY; |
| 2066 | if (copy_from_user(&val, optval, sizeof(val))) |
| 2067 | return -EFAULT; |
| 2068 | po->tp_reserve = val; |
| 2069 | return 0; |
| 2070 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2071 | case PACKET_LOSS: |
| 2072 | { |
| 2073 | unsigned int val; |
| 2074 | |
| 2075 | if (optlen != sizeof(val)) |
| 2076 | return -EINVAL; |
| 2077 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) |
| 2078 | return -EBUSY; |
| 2079 | if (copy_from_user(&val, optval, sizeof(val))) |
| 2080 | return -EFAULT; |
| 2081 | po->tp_loss = !!val; |
| 2082 | return 0; |
| 2083 | } |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 2084 | case PACKET_AUXDATA: |
| 2085 | { |
| 2086 | int val; |
| 2087 | |
| 2088 | if (optlen < sizeof(val)) |
| 2089 | return -EINVAL; |
| 2090 | if (copy_from_user(&val, optval, sizeof(val))) |
| 2091 | return -EFAULT; |
| 2092 | |
| 2093 | po->auxdata = !!val; |
| 2094 | return 0; |
| 2095 | } |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 2096 | case PACKET_ORIGDEV: |
| 2097 | { |
| 2098 | int val; |
| 2099 | |
| 2100 | if (optlen < sizeof(val)) |
| 2101 | return -EINVAL; |
| 2102 | if (copy_from_user(&val, optval, sizeof(val))) |
| 2103 | return -EFAULT; |
| 2104 | |
| 2105 | po->origdev = !!val; |
| 2106 | return 0; |
| 2107 | } |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2108 | case PACKET_VNET_HDR: |
| 2109 | { |
| 2110 | int val; |
| 2111 | |
| 2112 | if (sock->type != SOCK_RAW) |
| 2113 | return -EINVAL; |
| 2114 | if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) |
| 2115 | return -EBUSY; |
| 2116 | if (optlen < sizeof(val)) |
| 2117 | return -EINVAL; |
| 2118 | if (copy_from_user(&val, optval, sizeof(val))) |
| 2119 | return -EFAULT; |
| 2120 | |
| 2121 | po->has_vnet_hdr = !!val; |
| 2122 | return 0; |
| 2123 | } |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 2124 | case PACKET_TIMESTAMP: |
| 2125 | { |
| 2126 | int val; |
| 2127 | |
| 2128 | if (optlen != sizeof(val)) |
| 2129 | return -EINVAL; |
| 2130 | if (copy_from_user(&val, optval, sizeof(val))) |
| 2131 | return -EFAULT; |
| 2132 | |
| 2133 | po->tp_tstamp = val; |
| 2134 | return 0; |
| 2135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | default: |
| 2137 | return -ENOPROTOOPT; |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | static int packet_getsockopt(struct socket *sock, int level, int optname, |
| 2142 | char __user *optval, int __user *optlen) |
| 2143 | { |
| 2144 | int len; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 2145 | int val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | struct sock *sk = sock->sk; |
| 2147 | struct packet_sock *po = pkt_sk(sk); |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 2148 | void *data; |
| 2149 | struct tpacket_stats st; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
| 2151 | if (level != SOL_PACKET) |
| 2152 | return -ENOPROTOOPT; |
| 2153 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 2154 | if (get_user(len, optlen)) |
| 2155 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | |
| 2157 | if (len < 0) |
| 2158 | return -EINVAL; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2159 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2160 | switch (optname) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | case PACKET_STATISTICS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | if (len > sizeof(struct tpacket_stats)) |
| 2163 | len = sizeof(struct tpacket_stats); |
| 2164 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 2165 | st = po->stats; |
| 2166 | memset(&po->stats, 0, sizeof(st)); |
| 2167 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 2168 | st.tp_packets += st.tp_drops; |
| 2169 | |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 2170 | data = &st; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | break; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 2172 | case PACKET_AUXDATA: |
| 2173 | if (len > sizeof(int)) |
| 2174 | len = sizeof(int); |
| 2175 | val = po->auxdata; |
| 2176 | |
| 2177 | data = &val; |
| 2178 | break; |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 2179 | case PACKET_ORIGDEV: |
| 2180 | if (len > sizeof(int)) |
| 2181 | len = sizeof(int); |
| 2182 | val = po->origdev; |
| 2183 | |
| 2184 | data = &val; |
| 2185 | break; |
Sridhar Samudrala | bfd5f4a | 2010-02-04 20:24:10 -0800 | [diff] [blame] | 2186 | case PACKET_VNET_HDR: |
| 2187 | if (len > sizeof(int)) |
| 2188 | len = sizeof(int); |
| 2189 | val = po->has_vnet_hdr; |
| 2190 | |
| 2191 | data = &val; |
| 2192 | break; |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2193 | case PACKET_VERSION: |
| 2194 | if (len > sizeof(int)) |
| 2195 | len = sizeof(int); |
| 2196 | val = po->tp_version; |
| 2197 | data = &val; |
| 2198 | break; |
| 2199 | case PACKET_HDRLEN: |
| 2200 | if (len > sizeof(int)) |
| 2201 | len = sizeof(int); |
| 2202 | if (copy_from_user(&val, optval, len)) |
| 2203 | return -EFAULT; |
| 2204 | switch (val) { |
| 2205 | case TPACKET_V1: |
| 2206 | val = sizeof(struct tpacket_hdr); |
| 2207 | break; |
| 2208 | case TPACKET_V2: |
| 2209 | val = sizeof(struct tpacket2_hdr); |
| 2210 | break; |
| 2211 | default: |
| 2212 | return -EINVAL; |
| 2213 | } |
| 2214 | data = &val; |
| 2215 | break; |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 2216 | case PACKET_RESERVE: |
| 2217 | if (len > sizeof(unsigned int)) |
| 2218 | len = sizeof(unsigned int); |
| 2219 | val = po->tp_reserve; |
| 2220 | data = &val; |
| 2221 | break; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2222 | case PACKET_LOSS: |
| 2223 | if (len > sizeof(unsigned int)) |
| 2224 | len = sizeof(unsigned int); |
| 2225 | val = po->tp_loss; |
| 2226 | data = &val; |
| 2227 | break; |
Scott McMillan | 614f60f | 2010-06-02 05:53:56 -0700 | [diff] [blame] | 2228 | case PACKET_TIMESTAMP: |
| 2229 | if (len > sizeof(int)) |
| 2230 | len = sizeof(int); |
| 2231 | val = po->tp_tstamp; |
| 2232 | data = &val; |
| 2233 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | default: |
| 2235 | return -ENOPROTOOPT; |
| 2236 | } |
| 2237 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 2238 | if (put_user(len, optlen)) |
| 2239 | return -EFAULT; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 2240 | if (copy_to_user(optval, data, len)) |
| 2241 | return -EFAULT; |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 2242 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | |
| 2246 | static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data) |
| 2247 | { |
| 2248 | struct sock *sk; |
| 2249 | struct hlist_node *node; |
Jason Lunz | ad93065 | 2007-02-20 23:19:54 -0800 | [diff] [blame] | 2250 | struct net_device *dev = data; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2251 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2253 | rcu_read_lock(); |
| 2254 | sk_for_each_rcu(sk, node, &net->packet.sklist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | struct packet_sock *po = pkt_sk(sk); |
| 2256 | |
| 2257 | switch (msg) { |
| 2258 | case NETDEV_UNREGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | if (po->mclist) |
| 2260 | packet_dev_mclist(dev, po->mclist, -1); |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 2261 | /* fallthrough */ |
| 2262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | case NETDEV_DOWN: |
| 2264 | if (dev->ifindex == po->ifindex) { |
| 2265 | spin_lock(&po->bind_lock); |
| 2266 | if (po->running) { |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 2267 | __unregister_prot_hook(sk, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | sk->sk_err = ENETDOWN; |
| 2269 | if (!sock_flag(sk, SOCK_DEAD)) |
| 2270 | sk->sk_error_report(sk); |
| 2271 | } |
| 2272 | if (msg == NETDEV_UNREGISTER) { |
| 2273 | po->ifindex = -1; |
Ben Greear | 160ff18 | 2011-06-01 07:18:52 +0000 | [diff] [blame] | 2274 | if (po->prot_hook.dev) |
| 2275 | dev_put(po->prot_hook.dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | po->prot_hook.dev = NULL; |
| 2277 | } |
| 2278 | spin_unlock(&po->bind_lock); |
| 2279 | } |
| 2280 | break; |
| 2281 | case NETDEV_UP: |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2282 | if (dev->ifindex == po->ifindex) { |
| 2283 | spin_lock(&po->bind_lock); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 2284 | if (po->num) |
| 2285 | register_prot_hook(sk); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2286 | spin_unlock(&po->bind_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | break; |
| 2289 | } |
| 2290 | } |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2291 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | return NOTIFY_DONE; |
| 2293 | } |
| 2294 | |
| 2295 | |
| 2296 | static int packet_ioctl(struct socket *sock, unsigned int cmd, |
| 2297 | unsigned long arg) |
| 2298 | { |
| 2299 | struct sock *sk = sock->sk; |
| 2300 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2301 | switch (cmd) { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2302 | case SIOCOUTQ: |
| 2303 | { |
| 2304 | int amount = sk_wmem_alloc_get(sk); |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 2305 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2306 | return put_user(amount, (int __user *)arg); |
| 2307 | } |
| 2308 | case SIOCINQ: |
| 2309 | { |
| 2310 | struct sk_buff *skb; |
| 2311 | int amount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2313 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 2314 | skb = skb_peek(&sk->sk_receive_queue); |
| 2315 | if (skb) |
| 2316 | amount = skb->len; |
| 2317 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 2318 | return put_user(amount, (int __user *)arg); |
| 2319 | } |
| 2320 | case SIOCGSTAMP: |
| 2321 | return sock_get_timestamp(sk, (struct timeval __user *)arg); |
| 2322 | case SIOCGSTAMPNS: |
| 2323 | return sock_get_timestampns(sk, (struct timespec __user *)arg); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | #ifdef CONFIG_INET |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2326 | case SIOCADDRT: |
| 2327 | case SIOCDELRT: |
| 2328 | case SIOCDARP: |
| 2329 | case SIOCGARP: |
| 2330 | case SIOCSARP: |
| 2331 | case SIOCGIFADDR: |
| 2332 | case SIOCSIFADDR: |
| 2333 | case SIOCGIFBRDADDR: |
| 2334 | case SIOCSIFBRDADDR: |
| 2335 | case SIOCGIFNETMASK: |
| 2336 | case SIOCSIFNETMASK: |
| 2337 | case SIOCGIFDSTADDR: |
| 2338 | case SIOCSIFDSTADDR: |
| 2339 | case SIOCSIFFLAGS: |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2340 | return inet_dgram_ops.ioctl(sock, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | #endif |
| 2342 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2343 | default: |
| 2344 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | } |
| 2346 | return 0; |
| 2347 | } |
| 2348 | |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2349 | static unsigned int packet_poll(struct file *file, struct socket *sock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | poll_table *wait) |
| 2351 | { |
| 2352 | struct sock *sk = sock->sk; |
| 2353 | struct packet_sock *po = pkt_sk(sk); |
| 2354 | unsigned int mask = datagram_poll(file, sock, wait); |
| 2355 | |
| 2356 | spin_lock_bh(&sk->sk_receive_queue.lock); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2357 | if (po->rx_ring.pg_vec) { |
| 2358 | if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | mask |= POLLIN | POLLRDNORM; |
| 2360 | } |
| 2361 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2362 | spin_lock_bh(&sk->sk_write_queue.lock); |
| 2363 | if (po->tx_ring.pg_vec) { |
| 2364 | if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE)) |
| 2365 | mask |= POLLOUT | POLLWRNORM; |
| 2366 | } |
| 2367 | spin_unlock_bh(&sk->sk_write_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | return mask; |
| 2369 | } |
| 2370 | |
| 2371 | |
| 2372 | /* Dirty? Well, I still did not learn better way to account |
| 2373 | * for user mmaps. |
| 2374 | */ |
| 2375 | |
| 2376 | static void packet_mm_open(struct vm_area_struct *vma) |
| 2377 | { |
| 2378 | struct file *file = vma->vm_file; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2379 | struct socket *sock = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | if (sk) |
| 2383 | atomic_inc(&pkt_sk(sk)->mapped); |
| 2384 | } |
| 2385 | |
| 2386 | static void packet_mm_close(struct vm_area_struct *vma) |
| 2387 | { |
| 2388 | struct file *file = vma->vm_file; |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2389 | struct socket *sock = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | if (sk) |
| 2393 | atomic_dec(&pkt_sk(sk)->mapped); |
| 2394 | } |
| 2395 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 2396 | static const struct vm_operations_struct packet_mmap_ops = { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2397 | .open = packet_mm_open, |
| 2398 | .close = packet_mm_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | }; |
| 2400 | |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2401 | static void free_pg_vec(struct pgv *pg_vec, unsigned int order, |
| 2402 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | { |
| 2404 | int i; |
| 2405 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2406 | for (i = 0; i < len; i++) { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2407 | if (likely(pg_vec[i].buffer)) { |
Changli Gao | c56b4d9 | 2010-12-01 02:52:57 +0000 | [diff] [blame] | 2408 | if (is_vmalloc_addr(pg_vec[i].buffer)) |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2409 | vfree(pg_vec[i].buffer); |
| 2410 | else |
| 2411 | free_pages((unsigned long)pg_vec[i].buffer, |
| 2412 | order); |
| 2413 | pg_vec[i].buffer = NULL; |
| 2414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | } |
| 2416 | kfree(pg_vec); |
| 2417 | } |
| 2418 | |
Changli Gao | c56b4d9 | 2010-12-01 02:52:57 +0000 | [diff] [blame] | 2419 | static inline char *alloc_one_pg_vec_page(unsigned long order) |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2420 | { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2421 | char *buffer = NULL; |
| 2422 | gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | |
| 2423 | __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY; |
Eric Dumazet | 719bfea | 2009-04-15 03:39:52 -0700 | [diff] [blame] | 2424 | |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2425 | buffer = (char *) __get_free_pages(gfp_flags, order); |
| 2426 | |
| 2427 | if (buffer) |
| 2428 | return buffer; |
| 2429 | |
| 2430 | /* |
| 2431 | * __get_free_pages failed, fall back to vmalloc |
| 2432 | */ |
Eric Dumazet | bbce5a5 | 2010-11-20 07:31:54 +0000 | [diff] [blame] | 2433 | buffer = vzalloc((1 << order) * PAGE_SIZE); |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2434 | |
| 2435 | if (buffer) |
| 2436 | return buffer; |
| 2437 | |
| 2438 | /* |
| 2439 | * vmalloc failed, lets dig into swap here |
| 2440 | */ |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2441 | gfp_flags &= ~__GFP_NORETRY; |
| 2442 | buffer = (char *)__get_free_pages(gfp_flags, order); |
| 2443 | if (buffer) |
| 2444 | return buffer; |
| 2445 | |
| 2446 | /* |
| 2447 | * complete and utter failure |
| 2448 | */ |
| 2449 | return NULL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2450 | } |
| 2451 | |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2452 | static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order) |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2453 | { |
| 2454 | unsigned int block_nr = req->tp_block_nr; |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2455 | struct pgv *pg_vec; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2456 | int i; |
| 2457 | |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2458 | pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL); |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2459 | if (unlikely(!pg_vec)) |
| 2460 | goto out; |
| 2461 | |
| 2462 | for (i = 0; i < block_nr; i++) { |
Changli Gao | c56b4d9 | 2010-12-01 02:52:57 +0000 | [diff] [blame] | 2463 | pg_vec[i].buffer = alloc_one_pg_vec_page(order); |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2464 | if (unlikely(!pg_vec[i].buffer)) |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2465 | goto out_free_pgvec; |
| 2466 | } |
| 2467 | |
| 2468 | out: |
| 2469 | return pg_vec; |
| 2470 | |
| 2471 | out_free_pgvec: |
| 2472 | free_pg_vec(pg_vec, order, block_nr); |
| 2473 | pg_vec = NULL; |
| 2474 | goto out; |
| 2475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2477 | static int packet_set_ring(struct sock *sk, struct tpacket_req *req, |
| 2478 | int closing, int tx_ring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2480 | struct pgv *pg_vec = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | struct packet_sock *po = pkt_sk(sk); |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 2482 | int was_running, order = 0; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2483 | struct packet_ring_buffer *rb; |
| 2484 | struct sk_buff_head *rb_queue; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 2485 | __be16 num; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2486 | int err; |
| 2487 | |
| 2488 | rb = tx_ring ? &po->tx_ring : &po->rx_ring; |
| 2489 | rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue; |
| 2490 | |
| 2491 | err = -EBUSY; |
| 2492 | if (!closing) { |
| 2493 | if (atomic_read(&po->mapped)) |
| 2494 | goto out; |
| 2495 | if (atomic_read(&rb->pending)) |
| 2496 | goto out; |
| 2497 | } |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | if (req->tp_block_nr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | /* Sanity tests and some calculations */ |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2501 | err = -EBUSY; |
| 2502 | if (unlikely(rb->pg_vec)) |
| 2503 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | |
Patrick McHardy | bbd6ef8 | 2008-07-14 22:50:15 -0700 | [diff] [blame] | 2505 | switch (po->tp_version) { |
| 2506 | case TPACKET_V1: |
| 2507 | po->tp_hdrlen = TPACKET_HDRLEN; |
| 2508 | break; |
| 2509 | case TPACKET_V2: |
| 2510 | po->tp_hdrlen = TPACKET2_HDRLEN; |
| 2511 | break; |
| 2512 | } |
| 2513 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2514 | err = -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2515 | if (unlikely((int)req->tp_block_size <= 0)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2516 | goto out; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2517 | if (unlikely(req->tp_block_size & (PAGE_SIZE - 1))) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2518 | goto out; |
Patrick McHardy | 8913336a | 2008-07-18 18:05:19 -0700 | [diff] [blame] | 2519 | if (unlikely(req->tp_frame_size < po->tp_hdrlen + |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2520 | po->tp_reserve)) |
| 2521 | goto out; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2522 | if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1))) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2523 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2525 | rb->frames_per_block = req->tp_block_size/req->tp_frame_size; |
| 2526 | if (unlikely(rb->frames_per_block <= 0)) |
| 2527 | goto out; |
| 2528 | if (unlikely((rb->frames_per_block * req->tp_block_nr) != |
| 2529 | req->tp_frame_nr)) |
| 2530 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | |
| 2532 | err = -ENOMEM; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2533 | order = get_order(req->tp_block_size); |
| 2534 | pg_vec = alloc_pg_vec(req, order); |
| 2535 | if (unlikely(!pg_vec)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | goto out; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2537 | } |
| 2538 | /* Done */ |
| 2539 | else { |
| 2540 | err = -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2541 | if (unlikely(req->tp_frame_nr)) |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2542 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | lock_sock(sk); |
| 2546 | |
| 2547 | /* Detach socket from network */ |
| 2548 | spin_lock(&po->bind_lock); |
| 2549 | was_running = po->running; |
| 2550 | num = po->num; |
| 2551 | if (was_running) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | po->num = 0; |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 2553 | __unregister_prot_hook(sk, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | } |
| 2555 | spin_unlock(&po->bind_lock); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | synchronize_net(); |
| 2558 | |
| 2559 | err = -EBUSY; |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 2560 | mutex_lock(&po->pg_vec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | if (closing || atomic_read(&po->mapped) == 0) { |
| 2562 | err = 0; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2563 | spin_lock_bh(&rb_queue->lock); |
Changli Gao | c053fd9 | 2010-12-10 16:02:20 -0800 | [diff] [blame] | 2564 | swap(rb->pg_vec, pg_vec); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2565 | rb->frame_max = (req->tp_frame_nr - 1); |
| 2566 | rb->head = 0; |
| 2567 | rb->frame_size = req->tp_frame_size; |
| 2568 | spin_unlock_bh(&rb_queue->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | |
Changli Gao | c053fd9 | 2010-12-10 16:02:20 -0800 | [diff] [blame] | 2570 | swap(rb->pg_vec_order, order); |
| 2571 | swap(rb->pg_vec_len, req->tp_block_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2573 | rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE; |
| 2574 | po->prot_hook.func = (po->rx_ring.pg_vec) ? |
| 2575 | tpacket_rcv : packet_rcv; |
| 2576 | skb_queue_purge(rb_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | if (atomic_read(&po->mapped)) |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2578 | pr_err("packet_mmap: vma is busy: %d\n", |
| 2579 | atomic_read(&po->mapped)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | } |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 2581 | mutex_unlock(&po->pg_vec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | |
| 2583 | spin_lock(&po->bind_lock); |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 2584 | if (was_running) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | po->num = num; |
David S. Miller | ce06b03 | 2011-07-04 01:44:29 -0700 | [diff] [blame^] | 2586 | register_prot_hook(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | } |
| 2588 | spin_unlock(&po->bind_lock); |
| 2589 | |
| 2590 | release_sock(sk); |
| 2591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | if (pg_vec) |
| 2593 | free_pg_vec(pg_vec, order, req->tp_block_nr); |
| 2594 | out: |
| 2595 | return err; |
| 2596 | } |
| 2597 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2598 | static int packet_mmap(struct file *file, struct socket *sock, |
| 2599 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | { |
| 2601 | struct sock *sk = sock->sk; |
| 2602 | struct packet_sock *po = pkt_sk(sk); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2603 | unsigned long size, expected_size; |
| 2604 | struct packet_ring_buffer *rb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | unsigned long start; |
| 2606 | int err = -EINVAL; |
| 2607 | int i; |
| 2608 | |
| 2609 | if (vma->vm_pgoff) |
| 2610 | return -EINVAL; |
| 2611 | |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 2612 | mutex_lock(&po->pg_vec_lock); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2613 | |
| 2614 | expected_size = 0; |
| 2615 | for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) { |
| 2616 | if (rb->pg_vec) { |
| 2617 | expected_size += rb->pg_vec_len |
| 2618 | * rb->pg_vec_pages |
| 2619 | * PAGE_SIZE; |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | if (expected_size == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | goto out; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2625 | |
| 2626 | size = vma->vm_end - vma->vm_start; |
| 2627 | if (size != expected_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | goto out; |
| 2629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | start = vma->vm_start; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2631 | for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) { |
| 2632 | if (rb->pg_vec == NULL) |
| 2633 | continue; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2634 | |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2635 | for (i = 0; i < rb->pg_vec_len; i++) { |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2636 | struct page *page; |
| 2637 | void *kaddr = rb->pg_vec[i].buffer; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2638 | int pg_num; |
| 2639 | |
Changli Gao | c56b4d9 | 2010-12-01 02:52:57 +0000 | [diff] [blame] | 2640 | for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) { |
| 2641 | page = pgv_to_page(kaddr); |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2642 | err = vm_insert_page(vma, start, page); |
| 2643 | if (unlikely(err)) |
| 2644 | goto out; |
| 2645 | start += PAGE_SIZE; |
Neil Horman | 0e3125c | 2010-11-16 10:26:47 -0800 | [diff] [blame] | 2646 | kaddr += PAGE_SIZE; |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2647 | } |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | } |
Johann Baudy | 69e3c75 | 2009-05-18 22:11:22 -0700 | [diff] [blame] | 2650 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 2651 | atomic_inc(&po->mapped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | vma->vm_ops = &packet_mmap_ops; |
| 2653 | err = 0; |
| 2654 | |
| 2655 | out: |
Herbert Xu | 905db44 | 2009-01-30 14:12:06 -0800 | [diff] [blame] | 2656 | mutex_unlock(&po->pg_vec_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | return err; |
| 2658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2660 | static const struct proto_ops packet_ops_spkt = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | .family = PF_PACKET, |
| 2662 | .owner = THIS_MODULE, |
| 2663 | .release = packet_release, |
| 2664 | .bind = packet_bind_spkt, |
| 2665 | .connect = sock_no_connect, |
| 2666 | .socketpair = sock_no_socketpair, |
| 2667 | .accept = sock_no_accept, |
| 2668 | .getname = packet_getname_spkt, |
| 2669 | .poll = datagram_poll, |
| 2670 | .ioctl = packet_ioctl, |
| 2671 | .listen = sock_no_listen, |
| 2672 | .shutdown = sock_no_shutdown, |
| 2673 | .setsockopt = sock_no_setsockopt, |
| 2674 | .getsockopt = sock_no_getsockopt, |
| 2675 | .sendmsg = packet_sendmsg_spkt, |
| 2676 | .recvmsg = packet_recvmsg, |
| 2677 | .mmap = sock_no_mmap, |
| 2678 | .sendpage = sock_no_sendpage, |
| 2679 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2681 | static const struct proto_ops packet_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | .family = PF_PACKET, |
| 2683 | .owner = THIS_MODULE, |
| 2684 | .release = packet_release, |
| 2685 | .bind = packet_bind, |
| 2686 | .connect = sock_no_connect, |
| 2687 | .socketpair = sock_no_socketpair, |
| 2688 | .accept = sock_no_accept, |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2689 | .getname = packet_getname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | .poll = packet_poll, |
| 2691 | .ioctl = packet_ioctl, |
| 2692 | .listen = sock_no_listen, |
| 2693 | .shutdown = sock_no_shutdown, |
| 2694 | .setsockopt = packet_setsockopt, |
| 2695 | .getsockopt = packet_getsockopt, |
| 2696 | .sendmsg = packet_sendmsg, |
| 2697 | .recvmsg = packet_recvmsg, |
| 2698 | .mmap = packet_mmap, |
| 2699 | .sendpage = sock_no_sendpage, |
| 2700 | }; |
| 2701 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 2702 | static const struct net_proto_family packet_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | .family = PF_PACKET, |
| 2704 | .create = packet_create, |
| 2705 | .owner = THIS_MODULE, |
| 2706 | }; |
| 2707 | |
| 2708 | static struct notifier_block packet_netdev_notifier = { |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2709 | .notifier_call = packet_notifier, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | }; |
| 2711 | |
| 2712 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | |
| 2714 | static void *packet_seq_start(struct seq_file *seq, loff_t *pos) |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2715 | __acquires(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 2717 | struct net *net = seq_file_net(seq); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2718 | |
| 2719 | rcu_read_lock(); |
| 2720 | return seq_hlist_start_head_rcu(&net->packet.sklist, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2724 | { |
Herbert Xu | 1bf4095 | 2007-12-16 14:04:02 -0800 | [diff] [blame] | 2725 | struct net *net = seq_file_net(seq); |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2726 | return seq_hlist_next_rcu(v, &net->packet.sklist, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | static void packet_seq_stop(struct seq_file *seq, void *v) |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2730 | __releases(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | { |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2732 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 2735 | static int packet_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | { |
| 2737 | if (v == SEQ_START_TOKEN) |
| 2738 | seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n"); |
| 2739 | else { |
Li Zefan | b7ceabd | 2010-02-08 23:19:29 +0000 | [diff] [blame] | 2740 | struct sock *s = sk_entry(v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | const struct packet_sock *po = pkt_sk(s); |
| 2742 | |
| 2743 | seq_printf(seq, |
Dan Rosenberg | 71338aa | 2011-05-23 12:17:35 +0000 | [diff] [blame] | 2744 | "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | s, |
| 2746 | atomic_read(&s->sk_refcnt), |
| 2747 | s->sk_type, |
| 2748 | ntohs(po->num), |
| 2749 | po->ifindex, |
| 2750 | po->running, |
| 2751 | atomic_read(&s->sk_rmem_alloc), |
| 2752 | sock_i_uid(s), |
Eric Dumazet | 40d4e3d | 2009-07-21 21:57:59 +0000 | [diff] [blame] | 2753 | sock_i_ino(s)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | return 0; |
| 2757 | } |
| 2758 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 2759 | static const struct seq_operations packet_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | .start = packet_seq_start, |
| 2761 | .next = packet_seq_next, |
| 2762 | .stop = packet_seq_stop, |
| 2763 | .show = packet_seq_show, |
| 2764 | }; |
| 2765 | |
| 2766 | static int packet_seq_open(struct inode *inode, struct file *file) |
| 2767 | { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 2768 | return seq_open_net(inode, file, &packet_seq_ops, |
| 2769 | sizeof(struct seq_net_private)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 2772 | static const struct file_operations packet_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | .owner = THIS_MODULE, |
| 2774 | .open = packet_seq_open, |
| 2775 | .read = seq_read, |
| 2776 | .llseek = seq_lseek, |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 2777 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | }; |
| 2779 | |
| 2780 | #endif |
| 2781 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2782 | static int __net_init packet_net_init(struct net *net) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 2783 | { |
stephen hemminger | 808f511 | 2010-02-22 07:57:18 +0000 | [diff] [blame] | 2784 | spin_lock_init(&net->packet.sklist_lock); |
Denis V. Lunev | 2aaef4e | 2007-12-11 04:19:54 -0800 | [diff] [blame] | 2785 | INIT_HLIST_HEAD(&net->packet.sklist); |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 2786 | |
| 2787 | if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops)) |
| 2788 | return -ENOMEM; |
| 2789 | |
| 2790 | return 0; |
| 2791 | } |
| 2792 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2793 | static void __net_exit packet_net_exit(struct net *net) |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 2794 | { |
| 2795 | proc_net_remove(net, "packet"); |
| 2796 | } |
| 2797 | |
| 2798 | static struct pernet_operations packet_net_ops = { |
| 2799 | .init = packet_net_init, |
| 2800 | .exit = packet_net_exit, |
| 2801 | }; |
| 2802 | |
| 2803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | static void __exit packet_exit(void) |
| 2805 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | unregister_netdevice_notifier(&packet_netdev_notifier); |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 2807 | unregister_pernet_subsys(&packet_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | sock_unregister(PF_PACKET); |
| 2809 | proto_unregister(&packet_proto); |
| 2810 | } |
| 2811 | |
| 2812 | static int __init packet_init(void) |
| 2813 | { |
| 2814 | int rc = proto_register(&packet_proto, 0); |
| 2815 | |
| 2816 | if (rc != 0) |
| 2817 | goto out; |
| 2818 | |
| 2819 | sock_register(&packet_family_ops); |
Denis V. Lunev | d12d01d | 2007-11-19 22:28:35 -0800 | [diff] [blame] | 2820 | register_pernet_subsys(&packet_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | register_netdevice_notifier(&packet_netdev_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | out: |
| 2823 | return rc; |
| 2824 | } |
| 2825 | |
| 2826 | module_init(packet_init); |
| 2827 | module_exit(packet_exit); |
| 2828 | MODULE_LICENSE("GPL"); |
| 2829 | MODULE_ALIAS_NETPROTO(PF_PACKET); |