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