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 | * |
| 8 | * Version: $Id: af_packet.c,v 1.61 2002/02/08 03:57:19 davem Exp $ |
| 9 | * |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 10 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 12 | * Alan Cox, <gw4pts@gw4pts.ampr.org> |
| 13 | * |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 14 | * Fixes: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Alan Cox : verify_area() now used correctly |
| 16 | * Alan Cox : new skbuff lists, look ma no backlogs! |
| 17 | * Alan Cox : tidied skbuff lists. |
| 18 | * Alan Cox : Now uses generic datagram routines I |
| 19 | * added. Also fixed the peek/read crash |
| 20 | * from all old Linux datagram code. |
| 21 | * Alan Cox : Uses the improved datagram code. |
| 22 | * Alan Cox : Added NULL's for socket options. |
| 23 | * Alan Cox : Re-commented the code. |
| 24 | * Alan Cox : Use new kernel side addressing |
| 25 | * Rob Janssen : Correct MTU usage. |
| 26 | * Dave Platt : Counter leaks caused by incorrect |
| 27 | * interrupt locking and some slightly |
| 28 | * dubious gcc output. Can you read |
| 29 | * compiler: it said _VOLATILE_ |
| 30 | * Richard Kooijman : Timestamp fixes. |
| 31 | * Alan Cox : New buffers. Use sk->mac.raw. |
| 32 | * Alan Cox : sendmsg/recvmsg support. |
| 33 | * Alan Cox : Protocol setting support |
| 34 | * Alexey Kuznetsov : Untied from IPv4 stack. |
| 35 | * Cyrus Durgin : Fixed kerneld for kmod. |
| 36 | * Michal Ostrowski : Module initialization cleanup. |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 37 | * Ulises Alonso : Frame number limit removal and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * packet_set_ring memory leak. |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 39 | * Eric Biederman : Allow for > 8 byte hardware addresses. |
| 40 | * The convention is that longer addresses |
| 41 | * will simply extend the hardware address |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 42 | * byte arrays at the end of sockaddr_ll |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 43 | * and packet_mreq. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * |
| 45 | * This program is free software; you can redistribute it and/or |
| 46 | * modify it under the terms of the GNU General Public License |
| 47 | * as published by the Free Software Foundation; either version |
| 48 | * 2 of the License, or (at your option) any later version. |
| 49 | * |
| 50 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <linux/mm.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 54 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/fcntl.h> |
| 56 | #include <linux/socket.h> |
| 57 | #include <linux/in.h> |
| 58 | #include <linux/inet.h> |
| 59 | #include <linux/netdevice.h> |
| 60 | #include <linux/if_packet.h> |
| 61 | #include <linux/wireless.h> |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 62 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/kmod.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 64 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <net/ip.h> |
| 66 | #include <net/protocol.h> |
| 67 | #include <linux/skbuff.h> |
| 68 | #include <net/sock.h> |
| 69 | #include <linux/errno.h> |
| 70 | #include <linux/timer.h> |
| 71 | #include <asm/system.h> |
| 72 | #include <asm/uaccess.h> |
| 73 | #include <asm/ioctls.h> |
| 74 | #include <asm/page.h> |
Al Viro | a1f8e7f | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 75 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <asm/io.h> |
| 77 | #include <linux/proc_fs.h> |
| 78 | #include <linux/seq_file.h> |
| 79 | #include <linux/poll.h> |
| 80 | #include <linux/module.h> |
| 81 | #include <linux/init.h> |
| 82 | |
| 83 | #ifdef CONFIG_INET |
| 84 | #include <net/inet_common.h> |
| 85 | #endif |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | Assumptions: |
| 89 | - if device has no dev->hard_header routine, it adds and removes ll header |
| 90 | inside itself. In this case ll header is invisible outside of device, |
| 91 | but higher levels still should reserve dev->hard_header_len. |
| 92 | Some devices are enough clever to reallocate skb, when header |
| 93 | will not fit to reserved space (tunnel), another ones are silly |
| 94 | (PPP). |
| 95 | - packet socket receives packets with pulled ll header, |
| 96 | so that SOCK_RAW should push it back. |
| 97 | |
| 98 | On receive: |
| 99 | ----------- |
| 100 | |
| 101 | Incoming, dev->hard_header!=NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 102 | mac_header -> ll header |
| 103 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | Outgoing, dev->hard_header!=NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 106 | mac_header -> ll header |
| 107 | data -> ll header |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | Incoming, dev->hard_header==NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 110 | mac_header -> UNKNOWN position. It is very likely, that it points to ll |
| 111 | header. PPP makes it, that is wrong, because introduce |
YOSHIFUJI Hideaki | db0c58f | 2007-07-19 10:44:35 +0900 | [diff] [blame] | 112 | assymetry between rx and tx paths. |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 113 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | Outgoing, dev->hard_header==NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 116 | mac_header -> data. ll header is still not built! |
| 117 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | Resume |
| 120 | If dev->hard_header==NULL we are unlikely to restore sensible ll header. |
| 121 | |
| 122 | |
| 123 | On transmit: |
| 124 | ------------ |
| 125 | |
| 126 | dev->hard_header != NULL |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 127 | mac_header -> ll header |
| 128 | data -> ll header |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | 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] | 131 | mac_header -> data |
| 132 | data -> data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | We should set nh.raw on output to correct posistion, |
| 135 | packet classifier depends on it. |
| 136 | */ |
| 137 | |
| 138 | /* List of all packet sockets. */ |
| 139 | static HLIST_HEAD(packet_sklist); |
| 140 | static DEFINE_RWLOCK(packet_sklist_lock); |
| 141 | |
| 142 | static atomic_t packet_socks_nr; |
| 143 | |
| 144 | |
| 145 | /* Private packet socket structures. */ |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | struct packet_mclist |
| 148 | { |
| 149 | struct packet_mclist *next; |
| 150 | int ifindex; |
| 151 | int count; |
| 152 | unsigned short type; |
| 153 | unsigned short alen; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 154 | unsigned char addr[MAX_ADDR_LEN]; |
| 155 | }; |
| 156 | /* identical to struct packet_mreq except it has |
| 157 | * a longer address field. |
| 158 | */ |
| 159 | struct packet_mreq_max |
| 160 | { |
| 161 | int mr_ifindex; |
| 162 | unsigned short mr_type; |
| 163 | unsigned short mr_alen; |
| 164 | unsigned char mr_address[MAX_ADDR_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | }; |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | #ifdef CONFIG_PACKET_MMAP |
| 168 | static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing); |
| 169 | #endif |
| 170 | |
| 171 | static void packet_flush_mclist(struct sock *sk); |
| 172 | |
| 173 | struct packet_sock { |
| 174 | /* struct sock has to be the first member of packet_sock */ |
| 175 | struct sock sk; |
| 176 | struct tpacket_stats stats; |
| 177 | #ifdef CONFIG_PACKET_MMAP |
| 178 | char * *pg_vec; |
| 179 | unsigned int head; |
| 180 | unsigned int frames_per_block; |
| 181 | unsigned int frame_size; |
| 182 | unsigned int frame_max; |
| 183 | int copy_thresh; |
| 184 | #endif |
| 185 | struct packet_type prot_hook; |
| 186 | spinlock_t bind_lock; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 187 | unsigned int running:1, /* prot_hook is attached*/ |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 188 | auxdata:1, |
| 189 | origdev:1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | int ifindex; /* bound device */ |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 191 | __be16 num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | struct packet_mclist *mclist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | #ifdef CONFIG_PACKET_MMAP |
| 194 | atomic_t mapped; |
| 195 | unsigned int pg_vec_order; |
| 196 | unsigned int pg_vec_pages; |
| 197 | unsigned int pg_vec_len; |
| 198 | #endif |
| 199 | }; |
| 200 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 201 | struct packet_skb_cb { |
| 202 | unsigned int origlen; |
| 203 | union { |
| 204 | struct sockaddr_pkt pkt; |
| 205 | struct sockaddr_ll ll; |
| 206 | } sa; |
| 207 | }; |
| 208 | |
| 209 | #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb)) |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | #ifdef CONFIG_PACKET_MMAP |
| 212 | |
Jason Lunz | ad93065 | 2007-02-20 23:19:54 -0800 | [diff] [blame] | 213 | static inline struct tpacket_hdr *packet_lookup_frame(struct packet_sock *po, unsigned int position) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
| 215 | unsigned int pg_vec_pos, frame_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | pg_vec_pos = position / po->frames_per_block; |
| 218 | frame_offset = position % po->frames_per_block; |
| 219 | |
Jason Lunz | ad93065 | 2007-02-20 23:19:54 -0800 | [diff] [blame] | 220 | return (struct tpacket_hdr *)(po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | #endif |
| 223 | |
| 224 | static inline struct packet_sock *pkt_sk(struct sock *sk) |
| 225 | { |
| 226 | return (struct packet_sock *)sk; |
| 227 | } |
| 228 | |
| 229 | static void packet_sock_destruct(struct sock *sk) |
| 230 | { |
| 231 | BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc)); |
| 232 | BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc)); |
| 233 | |
| 234 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 235 | printk("Attempt to release alive packet socket: %p\n", sk); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | atomic_dec(&packet_socks_nr); |
| 240 | #ifdef PACKET_REFCNT_DEBUG |
| 241 | printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr)); |
| 242 | #endif |
| 243 | } |
| 244 | |
| 245 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 246 | static const struct proto_ops packet_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 248 | static const struct proto_ops packet_ops_spkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 250 | static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | struct sock *sk; |
| 253 | struct sockaddr_pkt *spkt; |
| 254 | |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 255 | if (dev->nd_net != &init_net) |
| 256 | goto out; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /* |
| 259 | * When we registered the protocol we saved the socket in the data |
| 260 | * field for just this event. |
| 261 | */ |
| 262 | |
| 263 | sk = pt->af_packet_priv; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* |
| 266 | * Yank back the headers [hope the device set this |
| 267 | * right or kerboom...] |
| 268 | * |
| 269 | * Incoming packets have ll header pulled, |
| 270 | * push it back. |
| 271 | * |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 272 | * For outgoing ones skb->data == skb_mac_header(skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | * so that this procedure is noop. |
| 274 | */ |
| 275 | |
| 276 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 277 | goto out; |
| 278 | |
| 279 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) |
| 280 | goto oom; |
| 281 | |
| 282 | /* drop any routing info */ |
| 283 | dst_release(skb->dst); |
| 284 | skb->dst = NULL; |
| 285 | |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 286 | /* drop conntrack reference */ |
| 287 | nf_reset(skb); |
| 288 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 289 | spkt = &PACKET_SKB_CB(skb)->sa.pkt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 291 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * The SOCK_PACKET socket receives _all_ frames. |
| 295 | */ |
| 296 | |
| 297 | spkt->spkt_family = dev->type; |
| 298 | strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device)); |
| 299 | spkt->spkt_protocol = skb->protocol; |
| 300 | |
| 301 | /* |
| 302 | * Charge the memory to the socket. This is done specifically |
| 303 | * to prevent sockets using all the memory up. |
| 304 | */ |
| 305 | |
| 306 | if (sock_queue_rcv_skb(sk,skb) == 0) |
| 307 | return 0; |
| 308 | |
| 309 | out: |
| 310 | kfree_skb(skb); |
| 311 | oom: |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /* |
| 317 | * Output a raw packet to a device layer. This bypasses all the other |
| 318 | * protocol layers and you must therefore supply it with a complete frame |
| 319 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock, |
| 322 | struct msghdr *msg, size_t len) |
| 323 | { |
| 324 | struct sock *sk = sock->sk; |
| 325 | struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name; |
| 326 | struct sk_buff *skb; |
| 327 | struct net_device *dev; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 328 | __be16 proto=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | int err; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 332 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | */ |
| 334 | |
| 335 | if (saddr) |
| 336 | { |
| 337 | if (msg->msg_namelen < sizeof(struct sockaddr)) |
| 338 | return(-EINVAL); |
| 339 | if (msg->msg_namelen==sizeof(struct sockaddr_pkt)) |
| 340 | proto=saddr->spkt_protocol; |
| 341 | } |
| 342 | else |
| 343 | return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */ |
| 344 | |
| 345 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 346 | * Find the device first to size check it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | */ |
| 348 | |
| 349 | saddr->spkt_device[13] = 0; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 350 | dev = dev_get_by_name(&init_net, saddr->spkt_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | err = -ENODEV; |
| 352 | if (dev == NULL) |
| 353 | goto out_unlock; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 354 | |
David S. Miller | d5e76b0 | 2007-01-25 19:30:36 -0800 | [diff] [blame] | 355 | err = -ENETDOWN; |
| 356 | if (!(dev->flags & IFF_UP)) |
| 357 | goto out_unlock; |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | /* |
| 360 | * You may not queue a frame bigger than the mtu. This is the lowest level |
| 361 | * raw protocol and you must do your own fragmentation at this level. |
| 362 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | err = -EMSGSIZE; |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 365 | if (len > dev->mtu + dev->hard_header_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | goto out_unlock; |
| 367 | |
| 368 | err = -ENOBUFS; |
| 369 | skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL); |
| 370 | |
| 371 | /* |
| 372 | * If the write buffer is full, then tough. At this level the user gets to |
| 373 | * deal with the problem - do your own algorithmic backoffs. That's far |
| 374 | * more flexible. |
| 375 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 376 | |
| 377 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | goto out_unlock; |
| 379 | |
| 380 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 381 | * Fill it in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* FIXME: Save some space for broken drivers that write a |
| 385 | * hard header at transmission time by themselves. PPP is the |
| 386 | * notable one here. This should really be fixed at the driver level. |
| 387 | */ |
| 388 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 389 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | /* Try to align data part correctly */ |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 392 | if (dev->header_ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | skb->data -= dev->hard_header_len; |
| 394 | skb->tail -= dev->hard_header_len; |
| 395 | if (len < dev->hard_header_len) |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 396 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Returns -EFAULT on error */ |
| 400 | err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len); |
| 401 | skb->protocol = proto; |
| 402 | skb->dev = dev; |
| 403 | skb->priority = sk->sk_priority; |
| 404 | if (err) |
| 405 | goto out_free; |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | /* |
| 408 | * Now send it |
| 409 | */ |
| 410 | |
| 411 | dev_queue_xmit(skb); |
| 412 | dev_put(dev); |
| 413 | return(len); |
| 414 | |
| 415 | out_free: |
| 416 | kfree_skb(skb); |
| 417 | out_unlock: |
| 418 | if (dev) |
| 419 | dev_put(dev); |
| 420 | return err; |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 423 | static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk, |
| 424 | unsigned int res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
| 426 | struct sk_filter *filter; |
| 427 | |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 428 | rcu_read_lock_bh(); |
| 429 | filter = rcu_dereference(sk->sk_filter); |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 430 | if (filter != NULL) |
| 431 | res = sk_run_filter(skb, filter->insns, filter->len); |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 432 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 434 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
| 438 | This function makes lazy skb cloning in hope that most of packets |
| 439 | are discarded by BPF. |
| 440 | |
| 441 | Note tricky part: we DO mangle shared skb! skb->data, skb->len |
| 442 | and skb->cb are mangled. It works because (and until) packets |
| 443 | falling here are owned by current CPU. Output packets are cloned |
| 444 | by dev_queue_xmit_nit(), input packets are processed by net_bh |
| 445 | sequencially, so that if we return skb to original state on exit, |
| 446 | we will not harm anyone. |
| 447 | */ |
| 448 | |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 449 | static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
| 451 | struct sock *sk; |
| 452 | struct sockaddr_ll *sll; |
| 453 | struct packet_sock *po; |
| 454 | u8 * skb_head = skb->data; |
| 455 | int skb_len = skb->len; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 456 | unsigned int snaplen, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 458 | if (dev->nd_net != &init_net) |
| 459 | goto drop; |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 462 | goto drop; |
| 463 | |
| 464 | sk = pt->af_packet_priv; |
| 465 | po = pkt_sk(sk); |
| 466 | |
| 467 | skb->dev = dev; |
| 468 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 469 | if (dev->header_ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /* The device has an explicit notion of ll header, |
| 471 | exported to higher levels. |
| 472 | |
| 473 | Otherwise, the device hides datails of it frame |
| 474 | structure, so that corresponding packet head |
| 475 | never delivered to user. |
| 476 | */ |
| 477 | if (sk->sk_type != SOCK_DGRAM) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 478 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | else if (skb->pkt_type == PACKET_OUTGOING) { |
| 480 | /* Special case: outgoing packets have ll header at head */ |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 481 | skb_pull(skb, skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | snaplen = skb->len; |
| 486 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 487 | res = run_filter(skb, sk, snaplen); |
| 488 | if (!res) |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 489 | goto drop_n_restore; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 490 | if (snaplen > res) |
| 491 | snaplen = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
| 493 | if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= |
| 494 | (unsigned)sk->sk_rcvbuf) |
| 495 | goto drop_n_acct; |
| 496 | |
| 497 | if (skb_shared(skb)) { |
| 498 | struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); |
| 499 | if (nskb == NULL) |
| 500 | goto drop_n_acct; |
| 501 | |
| 502 | if (skb_head != skb->data) { |
| 503 | skb->data = skb_head; |
| 504 | skb->len = skb_len; |
| 505 | } |
| 506 | kfree_skb(skb); |
| 507 | skb = nskb; |
| 508 | } |
| 509 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 510 | BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 > |
| 511 | sizeof(skb->cb)); |
| 512 | |
| 513 | sll = &PACKET_SKB_CB(skb)->sa.ll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | sll->sll_family = AF_PACKET; |
| 515 | sll->sll_hatype = dev->type; |
| 516 | sll->sll_protocol = skb->protocol; |
| 517 | sll->sll_pkttype = skb->pkt_type; |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 518 | if (unlikely(po->origdev) && skb->pkt_type == PACKET_HOST) |
| 519 | sll->sll_ifindex = orig_dev->ifindex; |
| 520 | else |
| 521 | sll->sll_ifindex = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 523 | sll->sll_halen = dev_parse_header(skb, sll->sll_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 525 | PACKET_SKB_CB(skb)->origlen = skb->len; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | if (pskb_trim(skb, snaplen)) |
| 528 | goto drop_n_acct; |
| 529 | |
| 530 | skb_set_owner_r(skb, sk); |
| 531 | skb->dev = NULL; |
| 532 | dst_release(skb->dst); |
| 533 | skb->dst = NULL; |
| 534 | |
Phil Oester | 84531c2 | 2005-07-12 11:57:52 -0700 | [diff] [blame] | 535 | /* drop conntrack reference */ |
| 536 | nf_reset(skb); |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | spin_lock(&sk->sk_receive_queue.lock); |
| 539 | po->stats.tp_packets++; |
| 540 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 541 | spin_unlock(&sk->sk_receive_queue.lock); |
| 542 | sk->sk_data_ready(sk, skb->len); |
| 543 | return 0; |
| 544 | |
| 545 | drop_n_acct: |
| 546 | spin_lock(&sk->sk_receive_queue.lock); |
| 547 | po->stats.tp_drops++; |
| 548 | spin_unlock(&sk->sk_receive_queue.lock); |
| 549 | |
| 550 | drop_n_restore: |
| 551 | if (skb_head != skb->data && skb_shared(skb)) { |
| 552 | skb->data = skb_head; |
| 553 | skb->len = skb_len; |
| 554 | } |
| 555 | drop: |
| 556 | kfree_skb(skb); |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | #ifdef CONFIG_PACKET_MMAP |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 561 | static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
| 563 | struct sock *sk; |
| 564 | struct packet_sock *po; |
| 565 | struct sockaddr_ll *sll; |
| 566 | struct tpacket_hdr *h; |
| 567 | u8 * skb_head = skb->data; |
| 568 | int skb_len = skb->len; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 569 | unsigned int snaplen, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER; |
| 571 | unsigned short macoff, netoff; |
| 572 | struct sk_buff *copy_skb = NULL; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 573 | struct timeval tv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
Eric W. Biederman | e730c15 | 2007-09-17 11:53:39 -0700 | [diff] [blame] | 575 | if (dev->nd_net != &init_net) |
| 576 | goto drop; |
| 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 579 | goto drop; |
| 580 | |
| 581 | sk = pt->af_packet_priv; |
| 582 | po = pkt_sk(sk); |
| 583 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 584 | if (dev->header_ops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (sk->sk_type != SOCK_DGRAM) |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 586 | skb_push(skb, skb->data - skb_mac_header(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | else if (skb->pkt_type == PACKET_OUTGOING) { |
| 588 | /* Special case: outgoing packets have ll header at head */ |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 589 | skb_pull(skb, skb_network_offset(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 593 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 594 | status |= TP_STATUS_CSUMNOTREADY; |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | snaplen = skb->len; |
| 597 | |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 598 | res = run_filter(skb, sk, snaplen); |
| 599 | if (!res) |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 600 | goto drop_n_restore; |
David S. Miller | dbcb585 | 2007-01-24 15:21:02 -0800 | [diff] [blame] | 601 | if (snaplen > res) |
| 602 | snaplen = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | if (sk->sk_type == SOCK_DGRAM) { |
| 605 | macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16; |
| 606 | } else { |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 607 | unsigned maclen = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen)); |
| 609 | macoff = netoff - maclen; |
| 610 | } |
| 611 | |
| 612 | if (macoff + snaplen > po->frame_size) { |
| 613 | if (po->copy_thresh && |
| 614 | atomic_read(&sk->sk_rmem_alloc) + skb->truesize < |
| 615 | (unsigned)sk->sk_rcvbuf) { |
| 616 | if (skb_shared(skb)) { |
| 617 | copy_skb = skb_clone(skb, GFP_ATOMIC); |
| 618 | } else { |
| 619 | copy_skb = skb_get(skb); |
| 620 | skb_head = skb->data; |
| 621 | } |
| 622 | if (copy_skb) |
| 623 | skb_set_owner_r(copy_skb, sk); |
| 624 | } |
| 625 | snaplen = po->frame_size - macoff; |
| 626 | if ((int)snaplen < 0) |
| 627 | snaplen = 0; |
| 628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | spin_lock(&sk->sk_receive_queue.lock); |
Jason Lunz | ad93065 | 2007-02-20 23:19:54 -0800 | [diff] [blame] | 631 | h = packet_lookup_frame(po, po->head); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | if (h->tp_status) |
| 634 | goto ring_is_full; |
| 635 | po->head = po->head != po->frame_max ? po->head+1 : 0; |
| 636 | po->stats.tp_packets++; |
| 637 | if (copy_skb) { |
| 638 | status |= TP_STATUS_COPY; |
| 639 | __skb_queue_tail(&sk->sk_receive_queue, copy_skb); |
| 640 | } |
| 641 | if (!po->stats.tp_drops) |
| 642 | status &= ~TP_STATUS_LOSING; |
| 643 | spin_unlock(&sk->sk_receive_queue.lock); |
| 644 | |
Patrick McHardy | cbe21d8 | 2006-09-17 23:59:57 -0700 | [diff] [blame] | 645 | skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | h->tp_len = skb->len; |
| 648 | h->tp_snaplen = snaplen; |
| 649 | h->tp_mac = macoff; |
| 650 | h->tp_net = netoff; |
Stephen Hemminger | 50f1778 | 2007-09-06 13:55:02 +0100 | [diff] [blame] | 651 | if (skb->tstamp.tv64) |
| 652 | tv = ktime_to_timeval(skb->tstamp); |
| 653 | else |
| 654 | do_gettimeofday(&tv); |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 655 | h->tp_sec = tv.tv_sec; |
| 656 | h->tp_usec = tv.tv_usec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h))); |
Stephen Hemminger | b95cce3 | 2007-09-26 22:13:38 -0700 | [diff] [blame] | 659 | sll->sll_halen = dev_parse_header(skb, sll->sll_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | sll->sll_family = AF_PACKET; |
| 661 | sll->sll_hatype = dev->type; |
| 662 | sll->sll_protocol = skb->protocol; |
| 663 | sll->sll_pkttype = skb->pkt_type; |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 664 | if (unlikely(po->origdev) && skb->pkt_type == PACKET_HOST) |
| 665 | sll->sll_ifindex = orig_dev->ifindex; |
| 666 | else |
| 667 | sll->sll_ifindex = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | h->tp_status = status; |
Ralf Baechle | e16aa20 | 2006-12-07 00:11:33 -0800 | [diff] [blame] | 670 | smp_mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | { |
| 673 | struct page *p_start, *p_end; |
| 674 | u8 *h_end = (u8 *)h + macoff + snaplen - 1; |
| 675 | |
| 676 | p_start = virt_to_page(h); |
| 677 | p_end = virt_to_page(h_end); |
| 678 | while (p_start <= p_end) { |
| 679 | flush_dcache_page(p_start); |
| 680 | p_start++; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | sk->sk_data_ready(sk, 0); |
| 685 | |
| 686 | drop_n_restore: |
| 687 | if (skb_head != skb->data && skb_shared(skb)) { |
| 688 | skb->data = skb_head; |
| 689 | skb->len = skb_len; |
| 690 | } |
| 691 | drop: |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 692 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | return 0; |
| 694 | |
| 695 | ring_is_full: |
| 696 | po->stats.tp_drops++; |
| 697 | spin_unlock(&sk->sk_receive_queue.lock); |
| 698 | |
| 699 | sk->sk_data_ready(sk, 0); |
| 700 | if (copy_skb) |
| 701 | kfree_skb(copy_skb); |
| 702 | goto drop_n_restore; |
| 703 | } |
| 704 | |
| 705 | #endif |
| 706 | |
| 707 | |
| 708 | static int packet_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 709 | struct msghdr *msg, size_t len) |
| 710 | { |
| 711 | struct sock *sk = sock->sk; |
| 712 | struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name; |
| 713 | struct sk_buff *skb; |
| 714 | struct net_device *dev; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 715 | __be16 proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | unsigned char *addr; |
| 717 | int ifindex, err, reserve = 0; |
| 718 | |
| 719 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 720 | * Get and verify the address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | if (saddr == NULL) { |
| 724 | struct packet_sock *po = pkt_sk(sk); |
| 725 | |
| 726 | ifindex = po->ifindex; |
| 727 | proto = po->num; |
| 728 | addr = NULL; |
| 729 | } else { |
| 730 | err = -EINVAL; |
| 731 | if (msg->msg_namelen < sizeof(struct sockaddr_ll)) |
| 732 | goto out; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 733 | if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) |
| 734 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | ifindex = saddr->sll_ifindex; |
| 736 | proto = saddr->sll_protocol; |
| 737 | addr = saddr->sll_addr; |
| 738 | } |
| 739 | |
| 740 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 741 | dev = dev_get_by_index(&init_net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | err = -ENXIO; |
| 743 | if (dev == NULL) |
| 744 | goto out_unlock; |
| 745 | if (sock->type == SOCK_RAW) |
| 746 | reserve = dev->hard_header_len; |
| 747 | |
David S. Miller | d5e76b0 | 2007-01-25 19:30:36 -0800 | [diff] [blame] | 748 | err = -ENETDOWN; |
| 749 | if (!(dev->flags & IFF_UP)) |
| 750 | goto out_unlock; |
| 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | err = -EMSGSIZE; |
| 753 | if (len > dev->mtu+reserve) |
| 754 | goto out_unlock; |
| 755 | |
| 756 | skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev), |
| 757 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 758 | if (skb==NULL) |
| 759 | goto out_unlock; |
| 760 | |
| 761 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 762 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Stephen Hemminger | 0c4e858 | 2007-10-09 01:36:32 -0700 | [diff] [blame] | 764 | err = -EINVAL; |
| 765 | if (sock->type == SOCK_DGRAM && |
| 766 | dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len) < 0) |
| 767 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | /* Returns -EFAULT on error */ |
| 770 | err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len); |
| 771 | if (err) |
| 772 | goto out_free; |
| 773 | |
| 774 | skb->protocol = proto; |
| 775 | skb->dev = dev; |
| 776 | skb->priority = sk->sk_priority; |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | /* |
| 779 | * Now send it |
| 780 | */ |
| 781 | |
| 782 | err = dev_queue_xmit(skb); |
| 783 | if (err > 0 && (err = net_xmit_errno(err)) != 0) |
| 784 | goto out_unlock; |
| 785 | |
| 786 | dev_put(dev); |
| 787 | |
| 788 | return(len); |
| 789 | |
| 790 | out_free: |
| 791 | kfree_skb(skb); |
| 792 | out_unlock: |
| 793 | if (dev) |
| 794 | dev_put(dev); |
| 795 | out: |
| 796 | return err; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Close a PACKET socket. This is fairly simple. We immediately go |
| 801 | * to 'closed' state and remove our protocol entry in the device list. |
| 802 | */ |
| 803 | |
| 804 | static int packet_release(struct socket *sock) |
| 805 | { |
| 806 | struct sock *sk = sock->sk; |
| 807 | struct packet_sock *po; |
| 808 | |
| 809 | if (!sk) |
| 810 | return 0; |
| 811 | |
| 812 | po = pkt_sk(sk); |
| 813 | |
| 814 | write_lock_bh(&packet_sklist_lock); |
| 815 | sk_del_node_init(sk); |
| 816 | write_unlock_bh(&packet_sklist_lock); |
| 817 | |
| 818 | /* |
| 819 | * Unhook packet receive handler. |
| 820 | */ |
| 821 | |
| 822 | if (po->running) { |
| 823 | /* |
| 824 | * Remove the protocol hook |
| 825 | */ |
| 826 | dev_remove_pack(&po->prot_hook); |
| 827 | po->running = 0; |
| 828 | po->num = 0; |
| 829 | __sock_put(sk); |
| 830 | } |
| 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | packet_flush_mclist(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
| 834 | #ifdef CONFIG_PACKET_MMAP |
| 835 | if (po->pg_vec) { |
| 836 | struct tpacket_req req; |
| 837 | memset(&req, 0, sizeof(req)); |
| 838 | packet_set_ring(sk, &req, 1); |
| 839 | } |
| 840 | #endif |
| 841 | |
| 842 | /* |
| 843 | * Now the socket is dead. No more input will appear. |
| 844 | */ |
| 845 | |
| 846 | sock_orphan(sk); |
| 847 | sock->sk = NULL; |
| 848 | |
| 849 | /* Purge queues */ |
| 850 | |
| 851 | skb_queue_purge(&sk->sk_receive_queue); |
| 852 | |
| 853 | sock_put(sk); |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Attach a packet hook. |
| 859 | */ |
| 860 | |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 861 | 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] | 862 | { |
| 863 | struct packet_sock *po = pkt_sk(sk); |
| 864 | /* |
| 865 | * Detach an existing hook if present. |
| 866 | */ |
| 867 | |
| 868 | lock_sock(sk); |
| 869 | |
| 870 | spin_lock(&po->bind_lock); |
| 871 | if (po->running) { |
| 872 | __sock_put(sk); |
| 873 | po->running = 0; |
| 874 | po->num = 0; |
| 875 | spin_unlock(&po->bind_lock); |
| 876 | dev_remove_pack(&po->prot_hook); |
| 877 | spin_lock(&po->bind_lock); |
| 878 | } |
| 879 | |
| 880 | po->num = protocol; |
| 881 | po->prot_hook.type = protocol; |
| 882 | po->prot_hook.dev = dev; |
| 883 | |
| 884 | po->ifindex = dev ? dev->ifindex : 0; |
| 885 | |
| 886 | if (protocol == 0) |
| 887 | goto out_unlock; |
| 888 | |
| 889 | if (dev) { |
| 890 | if (dev->flags&IFF_UP) { |
| 891 | dev_add_pack(&po->prot_hook); |
| 892 | sock_hold(sk); |
| 893 | po->running = 1; |
| 894 | } else { |
| 895 | sk->sk_err = ENETDOWN; |
| 896 | if (!sock_flag(sk, SOCK_DEAD)) |
| 897 | sk->sk_error_report(sk); |
| 898 | } |
| 899 | } else { |
| 900 | dev_add_pack(&po->prot_hook); |
| 901 | sock_hold(sk); |
| 902 | po->running = 1; |
| 903 | } |
| 904 | |
| 905 | out_unlock: |
| 906 | spin_unlock(&po->bind_lock); |
| 907 | release_sock(sk); |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Bind a packet socket to a device |
| 913 | */ |
| 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 916 | { |
| 917 | struct sock *sk=sock->sk; |
| 918 | char name[15]; |
| 919 | struct net_device *dev; |
| 920 | int err = -ENODEV; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | /* |
| 923 | * Check legality |
| 924 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 925 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 926 | if (addr_len != sizeof(struct sockaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return -EINVAL; |
| 928 | strlcpy(name,uaddr->sa_data,sizeof(name)); |
| 929 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 930 | dev = dev_get_by_name(&init_net, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | if (dev) { |
| 932 | err = packet_do_bind(sk, dev, pkt_sk(sk)->num); |
| 933 | dev_put(dev); |
| 934 | } |
| 935 | return err; |
| 936 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
| 939 | { |
| 940 | struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr; |
| 941 | struct sock *sk=sock->sk; |
| 942 | struct net_device *dev = NULL; |
| 943 | int err; |
| 944 | |
| 945 | |
| 946 | /* |
| 947 | * Check legality |
| 948 | */ |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | if (addr_len < sizeof(struct sockaddr_ll)) |
| 951 | return -EINVAL; |
| 952 | if (sll->sll_family != AF_PACKET) |
| 953 | return -EINVAL; |
| 954 | |
| 955 | if (sll->sll_ifindex) { |
| 956 | err = -ENODEV; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 957 | dev = dev_get_by_index(&init_net, sll->sll_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (dev == NULL) |
| 959 | goto out; |
| 960 | } |
| 961 | err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num); |
| 962 | if (dev) |
| 963 | dev_put(dev); |
| 964 | |
| 965 | out: |
| 966 | return err; |
| 967 | } |
| 968 | |
| 969 | static struct proto packet_proto = { |
| 970 | .name = "PACKET", |
| 971 | .owner = THIS_MODULE, |
| 972 | .obj_size = sizeof(struct packet_sock), |
| 973 | }; |
| 974 | |
| 975 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 976 | * Create a packet of type SOCK_PACKET. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | */ |
| 978 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 979 | static int packet_create(struct net *net, struct socket *sock, int protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | { |
| 981 | struct sock *sk; |
| 982 | struct packet_sock *po; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 983 | __be16 proto = (__force __be16)protocol; /* weird, but documented */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | int err; |
| 985 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 986 | if (net != &init_net) |
| 987 | return -EAFNOSUPPORT; |
| 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | if (!capable(CAP_NET_RAW)) |
| 990 | return -EPERM; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 991 | if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW && |
| 992 | sock->type != SOCK_PACKET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | return -ESOCKTNOSUPPORT; |
| 994 | |
| 995 | sock->state = SS_UNCONNECTED; |
| 996 | |
| 997 | err = -ENOBUFS; |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 998 | sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | if (sk == NULL) |
| 1000 | goto out; |
| 1001 | |
| 1002 | sock->ops = &packet_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | if (sock->type == SOCK_PACKET) |
| 1004 | sock->ops = &packet_ops_spkt; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | sock_init_data(sock, sk); |
| 1007 | |
| 1008 | po = pkt_sk(sk); |
| 1009 | sk->sk_family = PF_PACKET; |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1010 | po->num = proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | |
| 1012 | sk->sk_destruct = packet_sock_destruct; |
| 1013 | atomic_inc(&packet_socks_nr); |
| 1014 | |
| 1015 | /* |
| 1016 | * Attach a protocol block |
| 1017 | */ |
| 1018 | |
| 1019 | spin_lock_init(&po->bind_lock); |
| 1020 | po->prot_hook.func = packet_rcv; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | if (sock->type == SOCK_PACKET) |
| 1023 | po->prot_hook.func = packet_rcv_spkt; |
David S. Miller | be02097 | 2007-05-29 13:16:31 -0700 | [diff] [blame] | 1024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | po->prot_hook.af_packet_priv = sk; |
| 1026 | |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1027 | if (proto) { |
| 1028 | po->prot_hook.type = proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | dev_add_pack(&po->prot_hook); |
| 1030 | sock_hold(sk); |
| 1031 | po->running = 1; |
| 1032 | } |
| 1033 | |
| 1034 | write_lock_bh(&packet_sklist_lock); |
| 1035 | sk_add_node(sk, &packet_sklist); |
| 1036 | write_unlock_bh(&packet_sklist_lock); |
| 1037 | return(0); |
| 1038 | out: |
| 1039 | return err; |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * Pull a packet from our receive queue and hand it to the user. |
| 1044 | * If necessary we block. |
| 1045 | */ |
| 1046 | |
| 1047 | static int packet_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1048 | struct msghdr *msg, size_t len, int flags) |
| 1049 | { |
| 1050 | struct sock *sk = sock->sk; |
| 1051 | struct sk_buff *skb; |
| 1052 | int copied, err; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1053 | struct sockaddr_ll *sll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
| 1055 | err = -EINVAL; |
| 1056 | if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT)) |
| 1057 | goto out; |
| 1058 | |
| 1059 | #if 0 |
| 1060 | /* What error should we return now? EUNATTACH? */ |
| 1061 | if (pkt_sk(sk)->ifindex < 0) |
| 1062 | return -ENODEV; |
| 1063 | #endif |
| 1064 | |
| 1065 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | * Call the generic datagram receiver. This handles all sorts |
| 1067 | * of horrible races and re-entrancy so we can forget about it |
| 1068 | * in the protocol layers. |
| 1069 | * |
| 1070 | * Now it will return ENETDOWN, if device have just gone down, |
| 1071 | * but then it will block. |
| 1072 | */ |
| 1073 | |
| 1074 | skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err); |
| 1075 | |
| 1076 | /* |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1077 | * An error occurred so return it. Because skb_recv_datagram() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | * handles the blocking we don't see and worry about blocking |
| 1079 | * retries. |
| 1080 | */ |
| 1081 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 1082 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | goto out; |
| 1084 | |
| 1085 | /* |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1086 | * If the address length field is there to be filled in, we fill |
| 1087 | * it in now. |
| 1088 | */ |
| 1089 | |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1090 | sll = &PACKET_SKB_CB(skb)->sa.ll; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1091 | if (sock->type == SOCK_PACKET) |
| 1092 | msg->msg_namelen = sizeof(struct sockaddr_pkt); |
| 1093 | else |
| 1094 | msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr); |
| 1095 | |
| 1096 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | * You lose any data beyond the buffer you gave. If it worries a |
| 1098 | * user program they can ask the device for its MTU anyway. |
| 1099 | */ |
| 1100 | |
| 1101 | copied = skb->len; |
| 1102 | if (copied > len) |
| 1103 | { |
| 1104 | copied=len; |
| 1105 | msg->msg_flags|=MSG_TRUNC; |
| 1106 | } |
| 1107 | |
| 1108 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1109 | if (err) |
| 1110 | goto out_free; |
| 1111 | |
| 1112 | sock_recv_timestamp(msg, sk, skb); |
| 1113 | |
| 1114 | if (msg->msg_name) |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1115 | memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, |
| 1116 | msg->msg_namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1118 | if (pkt_sk(sk)->auxdata) { |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1119 | struct tpacket_auxdata aux; |
| 1120 | |
| 1121 | aux.tp_status = TP_STATUS_USER; |
| 1122 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1123 | aux.tp_status |= TP_STATUS_CSUMNOTREADY; |
| 1124 | aux.tp_len = PACKET_SKB_CB(skb)->origlen; |
| 1125 | aux.tp_snaplen = skb->len; |
| 1126 | aux.tp_mac = 0; |
Arnaldo Carvalho de Melo | bbe735e | 2007-03-10 22:16:10 -0300 | [diff] [blame] | 1127 | aux.tp_net = skb_network_offset(skb); |
Herbert Xu | ffbc611 | 2007-02-04 23:33:10 -0800 | [diff] [blame] | 1128 | |
| 1129 | put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux); |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | /* |
| 1133 | * Free or return the buffer as appropriate. Again this |
| 1134 | * hides all the races and re-entrancy issues from us. |
| 1135 | */ |
| 1136 | err = (flags&MSG_TRUNC) ? skb->len : copied; |
| 1137 | |
| 1138 | out_free: |
| 1139 | skb_free_datagram(sk, skb); |
| 1140 | out: |
| 1141 | return err; |
| 1142 | } |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr, |
| 1145 | int *uaddr_len, int peer) |
| 1146 | { |
| 1147 | struct net_device *dev; |
| 1148 | struct sock *sk = sock->sk; |
| 1149 | |
| 1150 | if (peer) |
| 1151 | return -EOPNOTSUPP; |
| 1152 | |
| 1153 | uaddr->sa_family = AF_PACKET; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1154 | dev = dev_get_by_index(&init_net, pkt_sk(sk)->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | if (dev) { |
| 1156 | strlcpy(uaddr->sa_data, dev->name, 15); |
| 1157 | dev_put(dev); |
| 1158 | } else |
| 1159 | memset(uaddr->sa_data, 0, 14); |
| 1160 | *uaddr_len = sizeof(*uaddr); |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
| 1165 | static int packet_getname(struct socket *sock, struct sockaddr *uaddr, |
| 1166 | int *uaddr_len, int peer) |
| 1167 | { |
| 1168 | struct net_device *dev; |
| 1169 | struct sock *sk = sock->sk; |
| 1170 | struct packet_sock *po = pkt_sk(sk); |
| 1171 | struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr; |
| 1172 | |
| 1173 | if (peer) |
| 1174 | return -EOPNOTSUPP; |
| 1175 | |
| 1176 | sll->sll_family = AF_PACKET; |
| 1177 | sll->sll_ifindex = po->ifindex; |
| 1178 | sll->sll_protocol = po->num; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1179 | dev = dev_get_by_index(&init_net, po->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | if (dev) { |
| 1181 | sll->sll_hatype = dev->type; |
| 1182 | sll->sll_halen = dev->addr_len; |
| 1183 | memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len); |
| 1184 | dev_put(dev); |
| 1185 | } else { |
| 1186 | sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */ |
| 1187 | sll->sll_halen = 0; |
| 1188 | } |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1189 | *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what) |
| 1195 | { |
| 1196 | switch (i->type) { |
| 1197 | case PACKET_MR_MULTICAST: |
| 1198 | if (what > 0) |
| 1199 | dev_mc_add(dev, i->addr, i->alen, 0); |
| 1200 | else |
| 1201 | dev_mc_delete(dev, i->addr, i->alen, 0); |
| 1202 | break; |
| 1203 | case PACKET_MR_PROMISC: |
| 1204 | dev_set_promiscuity(dev, what); |
| 1205 | break; |
| 1206 | case PACKET_MR_ALLMULTI: |
| 1207 | dev_set_allmulti(dev, what); |
| 1208 | break; |
| 1209 | default:; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what) |
| 1214 | { |
| 1215 | for ( ; i; i=i->next) { |
| 1216 | if (i->ifindex == dev->ifindex) |
| 1217 | packet_dev_mc(dev, i, what); |
| 1218 | } |
| 1219 | } |
| 1220 | |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1221 | 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] | 1222 | { |
| 1223 | struct packet_sock *po = pkt_sk(sk); |
| 1224 | struct packet_mclist *ml, *i; |
| 1225 | struct net_device *dev; |
| 1226 | int err; |
| 1227 | |
| 1228 | rtnl_lock(); |
| 1229 | |
| 1230 | err = -ENODEV; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1231 | dev = __dev_get_by_index(&init_net, mreq->mr_ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | if (!dev) |
| 1233 | goto done; |
| 1234 | |
| 1235 | err = -EINVAL; |
| 1236 | if (mreq->mr_alen > dev->addr_len) |
| 1237 | goto done; |
| 1238 | |
| 1239 | err = -ENOBUFS; |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 1240 | i = kmalloc(sizeof(*i), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | if (i == NULL) |
| 1242 | goto done; |
| 1243 | |
| 1244 | err = 0; |
| 1245 | for (ml = po->mclist; ml; ml = ml->next) { |
| 1246 | if (ml->ifindex == mreq->mr_ifindex && |
| 1247 | ml->type == mreq->mr_type && |
| 1248 | ml->alen == mreq->mr_alen && |
| 1249 | memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { |
| 1250 | ml->count++; |
| 1251 | /* Free the new element ... */ |
| 1252 | kfree(i); |
| 1253 | goto done; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | i->type = mreq->mr_type; |
| 1258 | i->ifindex = mreq->mr_ifindex; |
| 1259 | i->alen = mreq->mr_alen; |
| 1260 | memcpy(i->addr, mreq->mr_address, i->alen); |
| 1261 | i->count = 1; |
| 1262 | i->next = po->mclist; |
| 1263 | po->mclist = i; |
| 1264 | packet_dev_mc(dev, i, +1); |
| 1265 | |
| 1266 | done: |
| 1267 | rtnl_unlock(); |
| 1268 | return err; |
| 1269 | } |
| 1270 | |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1271 | 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] | 1272 | { |
| 1273 | struct packet_mclist *ml, **mlp; |
| 1274 | |
| 1275 | rtnl_lock(); |
| 1276 | |
| 1277 | for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) { |
| 1278 | if (ml->ifindex == mreq->mr_ifindex && |
| 1279 | ml->type == mreq->mr_type && |
| 1280 | ml->alen == mreq->mr_alen && |
| 1281 | memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { |
| 1282 | if (--ml->count == 0) { |
| 1283 | struct net_device *dev; |
| 1284 | *mlp = ml->next; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1285 | dev = dev_get_by_index(&init_net, ml->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | if (dev) { |
| 1287 | packet_dev_mc(dev, ml, -1); |
| 1288 | dev_put(dev); |
| 1289 | } |
| 1290 | kfree(ml); |
| 1291 | } |
| 1292 | rtnl_unlock(); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | } |
| 1296 | rtnl_unlock(); |
| 1297 | return -EADDRNOTAVAIL; |
| 1298 | } |
| 1299 | |
| 1300 | static void packet_flush_mclist(struct sock *sk) |
| 1301 | { |
| 1302 | struct packet_sock *po = pkt_sk(sk); |
| 1303 | struct packet_mclist *ml; |
| 1304 | |
| 1305 | if (!po->mclist) |
| 1306 | return; |
| 1307 | |
| 1308 | rtnl_lock(); |
| 1309 | while ((ml = po->mclist) != NULL) { |
| 1310 | struct net_device *dev; |
| 1311 | |
| 1312 | po->mclist = ml->next; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1313 | if ((dev = dev_get_by_index(&init_net, ml->ifindex)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | packet_dev_mc(dev, ml, -1); |
| 1315 | dev_put(dev); |
| 1316 | } |
| 1317 | kfree(ml); |
| 1318 | } |
| 1319 | rtnl_unlock(); |
| 1320 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | static int |
| 1323 | packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen) |
| 1324 | { |
| 1325 | struct sock *sk = sock->sk; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1326 | struct packet_sock *po = pkt_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | int ret; |
| 1328 | |
| 1329 | if (level != SOL_PACKET) |
| 1330 | return -ENOPROTOOPT; |
| 1331 | |
| 1332 | switch(optname) { |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1333 | case PACKET_ADD_MEMBERSHIP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | case PACKET_DROP_MEMBERSHIP: |
| 1335 | { |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1336 | struct packet_mreq_max mreq; |
| 1337 | int len = optlen; |
| 1338 | memset(&mreq, 0, sizeof(mreq)); |
| 1339 | if (len < sizeof(struct packet_mreq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | return -EINVAL; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1341 | if (len > sizeof(mreq)) |
| 1342 | len = sizeof(mreq); |
| 1343 | if (copy_from_user(&mreq,optval,len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | return -EFAULT; |
Eric W. Biederman | 0fb375f | 2005-09-21 00:11:37 -0700 | [diff] [blame] | 1345 | if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address))) |
| 1346 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | if (optname == PACKET_ADD_MEMBERSHIP) |
| 1348 | ret = packet_mc_add(sk, &mreq); |
| 1349 | else |
| 1350 | ret = packet_mc_drop(sk, &mreq); |
| 1351 | return ret; |
| 1352 | } |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 1353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | #ifdef CONFIG_PACKET_MMAP |
| 1355 | case PACKET_RX_RING: |
| 1356 | { |
| 1357 | struct tpacket_req req; |
| 1358 | |
| 1359 | if (optlen<sizeof(req)) |
| 1360 | return -EINVAL; |
| 1361 | if (copy_from_user(&req,optval,sizeof(req))) |
| 1362 | return -EFAULT; |
| 1363 | return packet_set_ring(sk, &req, 0); |
| 1364 | } |
| 1365 | case PACKET_COPY_THRESH: |
| 1366 | { |
| 1367 | int val; |
| 1368 | |
| 1369 | if (optlen!=sizeof(val)) |
| 1370 | return -EINVAL; |
| 1371 | if (copy_from_user(&val,optval,sizeof(val))) |
| 1372 | return -EFAULT; |
| 1373 | |
| 1374 | pkt_sk(sk)->copy_thresh = val; |
| 1375 | return 0; |
| 1376 | } |
| 1377 | #endif |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1378 | case PACKET_AUXDATA: |
| 1379 | { |
| 1380 | int val; |
| 1381 | |
| 1382 | if (optlen < sizeof(val)) |
| 1383 | return -EINVAL; |
| 1384 | if (copy_from_user(&val, optval, sizeof(val))) |
| 1385 | return -EFAULT; |
| 1386 | |
| 1387 | po->auxdata = !!val; |
| 1388 | return 0; |
| 1389 | } |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 1390 | case PACKET_ORIGDEV: |
| 1391 | { |
| 1392 | int val; |
| 1393 | |
| 1394 | if (optlen < sizeof(val)) |
| 1395 | return -EINVAL; |
| 1396 | if (copy_from_user(&val, optval, sizeof(val))) |
| 1397 | return -EFAULT; |
| 1398 | |
| 1399 | po->origdev = !!val; |
| 1400 | return 0; |
| 1401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | default: |
| 1403 | return -ENOPROTOOPT; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | static int packet_getsockopt(struct socket *sock, int level, int optname, |
| 1408 | char __user *optval, int __user *optlen) |
| 1409 | { |
| 1410 | int len; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1411 | int val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | struct sock *sk = sock->sk; |
| 1413 | struct packet_sock *po = pkt_sk(sk); |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1414 | void *data; |
| 1415 | struct tpacket_stats st; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | if (level != SOL_PACKET) |
| 1418 | return -ENOPROTOOPT; |
| 1419 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 1420 | if (get_user(len, optlen)) |
| 1421 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | |
| 1423 | if (len < 0) |
| 1424 | return -EINVAL; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | switch(optname) { |
| 1427 | case PACKET_STATISTICS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | if (len > sizeof(struct tpacket_stats)) |
| 1429 | len = sizeof(struct tpacket_stats); |
| 1430 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 1431 | st = po->stats; |
| 1432 | memset(&po->stats, 0, sizeof(st)); |
| 1433 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1434 | st.tp_packets += st.tp_drops; |
| 1435 | |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1436 | data = &st; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | break; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1438 | case PACKET_AUXDATA: |
| 1439 | if (len > sizeof(int)) |
| 1440 | len = sizeof(int); |
| 1441 | val = po->auxdata; |
| 1442 | |
| 1443 | data = &val; |
| 1444 | break; |
Peter P. Waskiewicz Jr | 80feaac | 2007-04-20 16:05:39 -0700 | [diff] [blame] | 1445 | case PACKET_ORIGDEV: |
| 1446 | if (len > sizeof(int)) |
| 1447 | len = sizeof(int); |
| 1448 | val = po->origdev; |
| 1449 | |
| 1450 | data = &val; |
| 1451 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | default: |
| 1453 | return -ENOPROTOOPT; |
| 1454 | } |
| 1455 | |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 1456 | if (put_user(len, optlen)) |
| 1457 | return -EFAULT; |
Herbert Xu | 8dc4194 | 2007-02-04 23:31:32 -0800 | [diff] [blame] | 1458 | if (copy_to_user(optval, data, len)) |
| 1459 | return -EFAULT; |
Kris Katterjohn | 8ae55f0 | 2006-01-23 16:28:02 -0800 | [diff] [blame] | 1460 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data) |
| 1465 | { |
| 1466 | struct sock *sk; |
| 1467 | struct hlist_node *node; |
Jason Lunz | ad93065 | 2007-02-20 23:19:54 -0800 | [diff] [blame] | 1468 | struct net_device *dev = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1470 | if (dev->nd_net != &init_net) |
| 1471 | return NOTIFY_DONE; |
| 1472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | read_lock(&packet_sklist_lock); |
| 1474 | sk_for_each(sk, node, &packet_sklist) { |
| 1475 | struct packet_sock *po = pkt_sk(sk); |
| 1476 | |
| 1477 | switch (msg) { |
| 1478 | case NETDEV_UNREGISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | if (po->mclist) |
| 1480 | packet_dev_mclist(dev, po->mclist, -1); |
David S. Miller | a2efcfa | 2007-05-29 13:12:50 -0700 | [diff] [blame] | 1481 | /* fallthrough */ |
| 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | case NETDEV_DOWN: |
| 1484 | if (dev->ifindex == po->ifindex) { |
| 1485 | spin_lock(&po->bind_lock); |
| 1486 | if (po->running) { |
| 1487 | __dev_remove_pack(&po->prot_hook); |
| 1488 | __sock_put(sk); |
| 1489 | po->running = 0; |
| 1490 | sk->sk_err = ENETDOWN; |
| 1491 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1492 | sk->sk_error_report(sk); |
| 1493 | } |
| 1494 | if (msg == NETDEV_UNREGISTER) { |
| 1495 | po->ifindex = -1; |
| 1496 | po->prot_hook.dev = NULL; |
| 1497 | } |
| 1498 | spin_unlock(&po->bind_lock); |
| 1499 | } |
| 1500 | break; |
| 1501 | case NETDEV_UP: |
| 1502 | spin_lock(&po->bind_lock); |
| 1503 | if (dev->ifindex == po->ifindex && po->num && |
| 1504 | !po->running) { |
| 1505 | dev_add_pack(&po->prot_hook); |
| 1506 | sock_hold(sk); |
| 1507 | po->running = 1; |
| 1508 | } |
| 1509 | spin_unlock(&po->bind_lock); |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | read_unlock(&packet_sklist_lock); |
| 1514 | return NOTIFY_DONE; |
| 1515 | } |
| 1516 | |
| 1517 | |
| 1518 | static int packet_ioctl(struct socket *sock, unsigned int cmd, |
| 1519 | unsigned long arg) |
| 1520 | { |
| 1521 | struct sock *sk = sock->sk; |
| 1522 | |
| 1523 | switch(cmd) { |
| 1524 | case SIOCOUTQ: |
| 1525 | { |
| 1526 | int amount = atomic_read(&sk->sk_wmem_alloc); |
| 1527 | return put_user(amount, (int __user *)arg); |
| 1528 | } |
| 1529 | case SIOCINQ: |
| 1530 | { |
| 1531 | struct sk_buff *skb; |
| 1532 | int amount = 0; |
| 1533 | |
| 1534 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 1535 | skb = skb_peek(&sk->sk_receive_queue); |
| 1536 | if (skb) |
| 1537 | amount = skb->len; |
| 1538 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1539 | return put_user(amount, (int __user *)arg); |
| 1540 | } |
| 1541 | case SIOCGSTAMP: |
| 1542 | return sock_get_timestamp(sk, (struct timeval __user *)arg); |
Eric Dumazet | ae40eb1 | 2007-03-18 17:33:16 -0700 | [diff] [blame] | 1543 | case SIOCGSTAMPNS: |
| 1544 | return sock_get_timestampns(sk, (struct timespec __user *)arg); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | #ifdef CONFIG_INET |
| 1547 | case SIOCADDRT: |
| 1548 | case SIOCDELRT: |
| 1549 | case SIOCDARP: |
| 1550 | case SIOCGARP: |
| 1551 | case SIOCSARP: |
| 1552 | case SIOCGIFADDR: |
| 1553 | case SIOCSIFADDR: |
| 1554 | case SIOCGIFBRDADDR: |
| 1555 | case SIOCSIFBRDADDR: |
| 1556 | case SIOCGIFNETMASK: |
| 1557 | case SIOCSIFNETMASK: |
| 1558 | case SIOCGIFDSTADDR: |
| 1559 | case SIOCSIFDSTADDR: |
| 1560 | case SIOCSIFFLAGS: |
| 1561 | return inet_dgram_ops.ioctl(sock, cmd, arg); |
| 1562 | #endif |
| 1563 | |
| 1564 | default: |
Christoph Hellwig | b5e5fa5 | 2006-01-03 14:18:33 -0800 | [diff] [blame] | 1565 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | } |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | #ifndef CONFIG_PACKET_MMAP |
| 1571 | #define packet_mmap sock_no_mmap |
| 1572 | #define packet_poll datagram_poll |
| 1573 | #else |
| 1574 | |
| 1575 | static unsigned int packet_poll(struct file * file, struct socket *sock, |
| 1576 | poll_table *wait) |
| 1577 | { |
| 1578 | struct sock *sk = sock->sk; |
| 1579 | struct packet_sock *po = pkt_sk(sk); |
| 1580 | unsigned int mask = datagram_poll(file, sock, wait); |
| 1581 | |
| 1582 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 1583 | if (po->pg_vec) { |
| 1584 | unsigned last = po->head ? po->head-1 : po->frame_max; |
| 1585 | struct tpacket_hdr *h; |
| 1586 | |
Jason Lunz | ad93065 | 2007-02-20 23:19:54 -0800 | [diff] [blame] | 1587 | h = packet_lookup_frame(po, last); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
| 1589 | if (h->tp_status) |
| 1590 | mask |= POLLIN | POLLRDNORM; |
| 1591 | } |
| 1592 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1593 | return mask; |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | /* Dirty? Well, I still did not learn better way to account |
| 1598 | * for user mmaps. |
| 1599 | */ |
| 1600 | |
| 1601 | static void packet_mm_open(struct vm_area_struct *vma) |
| 1602 | { |
| 1603 | struct file *file = vma->vm_file; |
Eric Dumazet | b69aee0 | 2005-09-06 14:42:45 -0700 | [diff] [blame] | 1604 | struct socket * sock = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | if (sk) |
| 1608 | atomic_inc(&pkt_sk(sk)->mapped); |
| 1609 | } |
| 1610 | |
| 1611 | static void packet_mm_close(struct vm_area_struct *vma) |
| 1612 | { |
| 1613 | struct file *file = vma->vm_file; |
Eric Dumazet | b69aee0 | 2005-09-06 14:42:45 -0700 | [diff] [blame] | 1614 | struct socket * sock = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | if (sk) |
| 1618 | atomic_dec(&pkt_sk(sk)->mapped); |
| 1619 | } |
| 1620 | |
| 1621 | static struct vm_operations_struct packet_mmap_ops = { |
| 1622 | .open = packet_mm_open, |
| 1623 | .close =packet_mm_close, |
| 1624 | }; |
| 1625 | |
| 1626 | static inline struct page *pg_vec_endpage(char *one_pg_vec, unsigned int order) |
| 1627 | { |
| 1628 | return virt_to_page(one_pg_vec + (PAGE_SIZE << order) - 1); |
| 1629 | } |
| 1630 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1631 | static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
| 1633 | int i; |
| 1634 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1635 | for (i = 0; i < len; i++) { |
| 1636 | if (likely(pg_vec[i])) |
| 1637 | free_pages((unsigned long) pg_vec[i], order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } |
| 1639 | kfree(pg_vec); |
| 1640 | } |
| 1641 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1642 | static inline char *alloc_one_pg_vec_page(unsigned long order) |
| 1643 | { |
| 1644 | return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO, |
| 1645 | order); |
| 1646 | } |
| 1647 | |
| 1648 | static char **alloc_pg_vec(struct tpacket_req *req, int order) |
| 1649 | { |
| 1650 | unsigned int block_nr = req->tp_block_nr; |
| 1651 | char **pg_vec; |
| 1652 | int i; |
| 1653 | |
| 1654 | pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL); |
| 1655 | if (unlikely(!pg_vec)) |
| 1656 | goto out; |
| 1657 | |
| 1658 | for (i = 0; i < block_nr; i++) { |
| 1659 | pg_vec[i] = alloc_one_pg_vec_page(order); |
| 1660 | if (unlikely(!pg_vec[i])) |
| 1661 | goto out_free_pgvec; |
| 1662 | } |
| 1663 | |
| 1664 | out: |
| 1665 | return pg_vec; |
| 1666 | |
| 1667 | out_free_pgvec: |
| 1668 | free_pg_vec(pg_vec, order, block_nr); |
| 1669 | pg_vec = NULL; |
| 1670 | goto out; |
| 1671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
| 1673 | static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing) |
| 1674 | { |
| 1675 | char **pg_vec = NULL; |
| 1676 | struct packet_sock *po = pkt_sk(sk); |
Al Viro | 0e11c91 | 2006-11-08 00:26:29 -0800 | [diff] [blame] | 1677 | int was_running, order = 0; |
| 1678 | __be16 num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | int err = 0; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | if (req->tp_block_nr) { |
| 1682 | int i, l; |
| 1683 | |
| 1684 | /* Sanity tests and some calculations */ |
| 1685 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1686 | if (unlikely(po->pg_vec)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | return -EBUSY; |
| 1688 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1689 | if (unlikely((int)req->tp_block_size <= 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | return -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1691 | if (unlikely(req->tp_block_size & (PAGE_SIZE - 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | return -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1693 | if (unlikely(req->tp_frame_size < TPACKET_HDRLEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | return -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1695 | if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | return -EINVAL; |
| 1697 | |
| 1698 | po->frames_per_block = req->tp_block_size/req->tp_frame_size; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1699 | if (unlikely(po->frames_per_block <= 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | return -EINVAL; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1701 | if (unlikely((po->frames_per_block * req->tp_block_nr) != |
| 1702 | req->tp_frame_nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | |
| 1705 | err = -ENOMEM; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1706 | order = get_order(req->tp_block_size); |
| 1707 | pg_vec = alloc_pg_vec(req, order); |
| 1708 | if (unlikely(!pg_vec)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | |
| 1711 | l = 0; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1712 | for (i = 0; i < req->tp_block_nr; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | char *ptr = pg_vec[i]; |
| 1714 | struct tpacket_hdr *header; |
| 1715 | int k; |
| 1716 | |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1717 | for (k = 0; k < po->frames_per_block; k++) { |
| 1718 | header = (struct tpacket_hdr *) ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | header->tp_status = TP_STATUS_KERNEL; |
| 1720 | ptr += req->tp_frame_size; |
| 1721 | } |
| 1722 | } |
| 1723 | /* Done */ |
| 1724 | } else { |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1725 | if (unlikely(req->tp_frame_nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | return -EINVAL; |
| 1727 | } |
| 1728 | |
| 1729 | lock_sock(sk); |
| 1730 | |
| 1731 | /* Detach socket from network */ |
| 1732 | spin_lock(&po->bind_lock); |
| 1733 | was_running = po->running; |
| 1734 | num = po->num; |
| 1735 | if (was_running) { |
| 1736 | __dev_remove_pack(&po->prot_hook); |
| 1737 | po->num = 0; |
| 1738 | po->running = 0; |
| 1739 | __sock_put(sk); |
| 1740 | } |
| 1741 | spin_unlock(&po->bind_lock); |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | synchronize_net(); |
| 1744 | |
| 1745 | err = -EBUSY; |
| 1746 | if (closing || atomic_read(&po->mapped) == 0) { |
| 1747 | err = 0; |
| 1748 | #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; }) |
| 1749 | |
| 1750 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 1751 | pg_vec = XC(po->pg_vec, pg_vec); |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1752 | po->frame_max = (req->tp_frame_nr - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | po->head = 0; |
| 1754 | po->frame_size = req->tp_frame_size; |
| 1755 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1756 | |
| 1757 | order = XC(po->pg_vec_order, order); |
| 1758 | req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr); |
| 1759 | |
| 1760 | po->pg_vec_pages = req->tp_block_size/PAGE_SIZE; |
| 1761 | po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv; |
| 1762 | skb_queue_purge(&sk->sk_receive_queue); |
| 1763 | #undef XC |
| 1764 | if (atomic_read(&po->mapped)) |
| 1765 | printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped)); |
| 1766 | } |
| 1767 | |
| 1768 | spin_lock(&po->bind_lock); |
| 1769 | if (was_running && !po->running) { |
| 1770 | sock_hold(sk); |
| 1771 | po->running = 1; |
| 1772 | po->num = num; |
| 1773 | dev_add_pack(&po->prot_hook); |
| 1774 | } |
| 1775 | spin_unlock(&po->bind_lock); |
| 1776 | |
| 1777 | release_sock(sk); |
| 1778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | if (pg_vec) |
| 1780 | free_pg_vec(pg_vec, order, req->tp_block_nr); |
| 1781 | out: |
| 1782 | return err; |
| 1783 | } |
| 1784 | |
| 1785 | static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma) |
| 1786 | { |
| 1787 | struct sock *sk = sock->sk; |
| 1788 | struct packet_sock *po = pkt_sk(sk); |
| 1789 | unsigned long size; |
| 1790 | unsigned long start; |
| 1791 | int err = -EINVAL; |
| 1792 | int i; |
| 1793 | |
| 1794 | if (vma->vm_pgoff) |
| 1795 | return -EINVAL; |
| 1796 | |
| 1797 | size = vma->vm_end - vma->vm_start; |
| 1798 | |
| 1799 | lock_sock(sk); |
| 1800 | if (po->pg_vec == NULL) |
| 1801 | goto out; |
| 1802 | if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE) |
| 1803 | goto out; |
| 1804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | start = vma->vm_start; |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1806 | for (i = 0; i < po->pg_vec_len; i++) { |
| 1807 | struct page *page = virt_to_page(po->pg_vec[i]); |
| 1808 | int pg_num; |
| 1809 | |
| 1810 | for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) { |
| 1811 | err = vm_insert_page(vma, start, page); |
| 1812 | if (unlikely(err)) |
| 1813 | goto out; |
| 1814 | start += PAGE_SIZE; |
| 1815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | } |
David S. Miller | 4ebf0ae | 2005-12-06 16:38:35 -0800 | [diff] [blame] | 1817 | atomic_inc(&po->mapped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | vma->vm_ops = &packet_mmap_ops; |
| 1819 | err = 0; |
| 1820 | |
| 1821 | out: |
| 1822 | release_sock(sk); |
| 1823 | return err; |
| 1824 | } |
| 1825 | #endif |
| 1826 | |
| 1827 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1828 | static const struct proto_ops packet_ops_spkt = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | .family = PF_PACKET, |
| 1830 | .owner = THIS_MODULE, |
| 1831 | .release = packet_release, |
| 1832 | .bind = packet_bind_spkt, |
| 1833 | .connect = sock_no_connect, |
| 1834 | .socketpair = sock_no_socketpair, |
| 1835 | .accept = sock_no_accept, |
| 1836 | .getname = packet_getname_spkt, |
| 1837 | .poll = datagram_poll, |
| 1838 | .ioctl = packet_ioctl, |
| 1839 | .listen = sock_no_listen, |
| 1840 | .shutdown = sock_no_shutdown, |
| 1841 | .setsockopt = sock_no_setsockopt, |
| 1842 | .getsockopt = sock_no_getsockopt, |
| 1843 | .sendmsg = packet_sendmsg_spkt, |
| 1844 | .recvmsg = packet_recvmsg, |
| 1845 | .mmap = sock_no_mmap, |
| 1846 | .sendpage = sock_no_sendpage, |
| 1847 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 1849 | static const struct proto_ops packet_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | .family = PF_PACKET, |
| 1851 | .owner = THIS_MODULE, |
| 1852 | .release = packet_release, |
| 1853 | .bind = packet_bind, |
| 1854 | .connect = sock_no_connect, |
| 1855 | .socketpair = sock_no_socketpair, |
| 1856 | .accept = sock_no_accept, |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1857 | .getname = packet_getname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | .poll = packet_poll, |
| 1859 | .ioctl = packet_ioctl, |
| 1860 | .listen = sock_no_listen, |
| 1861 | .shutdown = sock_no_shutdown, |
| 1862 | .setsockopt = packet_setsockopt, |
| 1863 | .getsockopt = packet_getsockopt, |
| 1864 | .sendmsg = packet_sendmsg, |
| 1865 | .recvmsg = packet_recvmsg, |
| 1866 | .mmap = packet_mmap, |
| 1867 | .sendpage = sock_no_sendpage, |
| 1868 | }; |
| 1869 | |
| 1870 | static struct net_proto_family packet_family_ops = { |
| 1871 | .family = PF_PACKET, |
| 1872 | .create = packet_create, |
| 1873 | .owner = THIS_MODULE, |
| 1874 | }; |
| 1875 | |
| 1876 | static struct notifier_block packet_netdev_notifier = { |
| 1877 | .notifier_call =packet_notifier, |
| 1878 | }; |
| 1879 | |
| 1880 | #ifdef CONFIG_PROC_FS |
| 1881 | static inline struct sock *packet_seq_idx(loff_t off) |
| 1882 | { |
| 1883 | struct sock *s; |
| 1884 | struct hlist_node *node; |
| 1885 | |
| 1886 | sk_for_each(s, node, &packet_sklist) { |
| 1887 | if (!off--) |
| 1888 | return s; |
| 1889 | } |
| 1890 | return NULL; |
| 1891 | } |
| 1892 | |
| 1893 | static void *packet_seq_start(struct seq_file *seq, loff_t *pos) |
| 1894 | { |
| 1895 | read_lock(&packet_sklist_lock); |
| 1896 | return *pos ? packet_seq_idx(*pos - 1) : SEQ_START_TOKEN; |
| 1897 | } |
| 1898 | |
| 1899 | static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1900 | { |
| 1901 | ++*pos; |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1902 | return (v == SEQ_START_TOKEN) |
| 1903 | ? sk_head(&packet_sklist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | : sk_next((struct sock*)v) ; |
| 1905 | } |
| 1906 | |
| 1907 | static void packet_seq_stop(struct seq_file *seq, void *v) |
| 1908 | { |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1909 | read_unlock(&packet_sklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
YOSHIFUJI Hideaki | 1ce4f28 | 2007-02-09 23:25:10 +0900 | [diff] [blame] | 1912 | static int packet_seq_show(struct seq_file *seq, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | { |
| 1914 | if (v == SEQ_START_TOKEN) |
| 1915 | seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n"); |
| 1916 | else { |
| 1917 | struct sock *s = v; |
| 1918 | const struct packet_sock *po = pkt_sk(s); |
| 1919 | |
| 1920 | seq_printf(seq, |
| 1921 | "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n", |
| 1922 | s, |
| 1923 | atomic_read(&s->sk_refcnt), |
| 1924 | s->sk_type, |
| 1925 | ntohs(po->num), |
| 1926 | po->ifindex, |
| 1927 | po->running, |
| 1928 | atomic_read(&s->sk_rmem_alloc), |
| 1929 | sock_i_uid(s), |
| 1930 | sock_i_ino(s) ); |
| 1931 | } |
| 1932 | |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1936 | static const struct seq_operations packet_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | .start = packet_seq_start, |
| 1938 | .next = packet_seq_next, |
| 1939 | .stop = packet_seq_stop, |
| 1940 | .show = packet_seq_show, |
| 1941 | }; |
| 1942 | |
| 1943 | static int packet_seq_open(struct inode *inode, struct file *file) |
| 1944 | { |
| 1945 | return seq_open(file, &packet_seq_ops); |
| 1946 | } |
| 1947 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 1948 | static const struct file_operations packet_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | .owner = THIS_MODULE, |
| 1950 | .open = packet_seq_open, |
| 1951 | .read = seq_read, |
| 1952 | .llseek = seq_lseek, |
| 1953 | .release = seq_release, |
| 1954 | }; |
| 1955 | |
| 1956 | #endif |
| 1957 | |
| 1958 | static void __exit packet_exit(void) |
| 1959 | { |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1960 | proc_net_remove(&init_net, "packet"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | unregister_netdevice_notifier(&packet_netdev_notifier); |
| 1962 | sock_unregister(PF_PACKET); |
| 1963 | proto_unregister(&packet_proto); |
| 1964 | } |
| 1965 | |
| 1966 | static int __init packet_init(void) |
| 1967 | { |
| 1968 | int rc = proto_register(&packet_proto, 0); |
| 1969 | |
| 1970 | if (rc != 0) |
| 1971 | goto out; |
| 1972 | |
| 1973 | sock_register(&packet_family_ops); |
| 1974 | register_netdevice_notifier(&packet_netdev_notifier); |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1975 | proc_net_fops_create(&init_net, "packet", 0, &packet_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | out: |
| 1977 | return rc; |
| 1978 | } |
| 1979 | |
| 1980 | module_init(packet_init); |
| 1981 | module_exit(packet_exit); |
| 1982 | MODULE_LICENSE("GPL"); |
| 1983 | MODULE_ALIAS_NETPROTO(PF_PACKET); |