af_packet.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:42k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.58 2001/11/28 21:02:10 davem Exp $
  9.  *
  10.  * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
  11.  * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12.  * Alan Cox, <gw4pts@gw4pts.ampr.org>
  13.  *
  14.  * Fixes:
  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.
  37.  *
  38.  * This program is free software; you can redistribute it and/or
  39.  * modify it under the terms of the GNU General Public License
  40.  * as published by the Free Software Foundation; either version
  41.  * 2 of the License, or (at your option) any later version.
  42.  *
  43.  */
  44.  
  45. #include <linux/config.h>
  46. #include <linux/types.h>
  47. #include <linux/sched.h>
  48. #include <linux/mm.h>
  49. #include <linux/fcntl.h>
  50. #include <linux/socket.h>
  51. #include <linux/in.h>
  52. #include <linux/inet.h>
  53. #include <linux/netdevice.h>
  54. #include <linux/if_packet.h>
  55. #include <linux/wireless.h>
  56. #include <linux/kmod.h>
  57. #include <net/ip.h>
  58. #include <net/protocol.h>
  59. #include <linux/skbuff.h>
  60. #include <net/sock.h>
  61. #include <linux/errno.h>
  62. #include <linux/timer.h>
  63. #include <asm/system.h>
  64. #include <asm/uaccess.h>
  65. #include <asm/ioctls.h>
  66. #include <linux/proc_fs.h>
  67. #include <linux/poll.h>
  68. #include <linux/module.h>
  69. #include <linux/init.h>
  70. #include <linux/if_bridge.h>
  71. #ifdef CONFIG_NET_DIVERT
  72. #include <linux/divert.h>
  73. #endif /* CONFIG_NET_DIVERT */
  74. #ifdef CONFIG_INET
  75. #include <net/inet_common.h>
  76. #endif
  77. #ifdef CONFIG_DLCI
  78. extern int dlci_ioctl(unsigned int, void*);
  79. #endif
  80. #define CONFIG_SOCK_PACKET 1
  81. /*
  82.    Proposed replacement for SIOC{ADD,DEL}MULTI and
  83.    IFF_PROMISC, IFF_ALLMULTI flags.
  84.    It is more expensive, but I believe,
  85.    it is really correct solution: reentereble, safe and fault tolerant.
  86.    IFF_PROMISC/IFF_ALLMULTI/SIOC{ADD/DEL}MULTI are faked by keeping
  87.    reference count and global flag, so that real status is
  88.    (gflag|(count != 0)), so that we can use obsolete faulty interface
  89.    not harming clever users.
  90.  */
  91. #define CONFIG_PACKET_MULTICAST 1
  92. /*
  93.    Assumptions:
  94.    - if device has no dev->hard_header routine, it adds and removes ll header
  95.      inside itself. In this case ll header is invisible outside of device,
  96.      but higher levels still should reserve dev->hard_header_len.
  97.      Some devices are enough clever to reallocate skb, when header
  98.      will not fit to reserved space (tunnel), another ones are silly
  99.      (PPP).
  100.    - packet socket receives packets with pulled ll header,
  101.      so that SOCK_RAW should push it back.
  102. On receive:
  103. -----------
  104. Incoming, dev->hard_header!=NULL
  105.    mac.raw -> ll header
  106.    data    -> data
  107. Outgoing, dev->hard_header!=NULL
  108.    mac.raw -> ll header
  109.    data    -> ll header
  110. Incoming, dev->hard_header==NULL
  111.    mac.raw -> UNKNOWN position. It is very likely, that it points to ll header.
  112.               PPP makes it, that is wrong, because introduce assymetry
  113.       between rx and tx paths.
  114.    data    -> data
  115. Outgoing, dev->hard_header==NULL
  116.    mac.raw -> data. ll header is still not built!
  117.    data    -> data
  118. Resume
  119.   If dev->hard_header==NULL we are unlikely to restore sensible ll header.
  120. On transmit:
  121. ------------
  122. dev->hard_header != NULL
  123.    mac.raw -> ll header
  124.    data    -> ll header
  125. dev->hard_header == NULL (ll header is added by device, we cannot control it)
  126.    mac.raw -> data
  127.    data -> data
  128.    We should set nh.raw on output to correct posistion,
  129.    packet classifier depends on it.
  130.  */
  131. /* List of all packet sockets. */
  132. static struct sock * packet_sklist;
  133. static rwlock_t packet_sklist_lock = RW_LOCK_UNLOCKED;
  134. atomic_t packet_socks_nr;
  135. /* Private packet socket structures. */
  136. #ifdef CONFIG_PACKET_MULTICAST
  137. struct packet_mclist
  138. {
  139. struct packet_mclist *next;
  140. int ifindex;
  141. int count;
  142. unsigned short type;
  143. unsigned short alen;
  144. unsigned char addr[8];
  145. };
  146. #endif
  147. #ifdef CONFIG_PACKET_MMAP
  148. static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);
  149. #endif
  150. static void packet_flush_mclist(struct sock *sk);
  151. struct packet_opt
  152. {
  153. struct packet_type prot_hook;
  154. spinlock_t bind_lock;
  155. char running; /* prot_hook is attached*/
  156. int ifindex; /* bound device */
  157. struct tpacket_stats stats;
  158. #ifdef CONFIG_PACKET_MULTICAST
  159. struct packet_mclist *mclist;
  160. #endif
  161. #ifdef CONFIG_PACKET_MMAP
  162. atomic_t mapped;
  163. unsigned long *pg_vec;
  164. unsigned int pg_vec_order;
  165. unsigned int pg_vec_pages;
  166. unsigned int pg_vec_len;
  167. struct tpacket_hdr **iovec;
  168. unsigned int frame_size;
  169. unsigned int iovmax;
  170. unsigned int head;
  171. int copy_thresh;
  172. #endif
  173. };
  174. void packet_sock_destruct(struct sock *sk)
  175. {
  176. BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
  177. BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
  178. if (!sk->dead) {
  179. printk("Attempt to release alive packet socket: %pn", sk);
  180. return;
  181. }
  182. if (sk->protinfo.destruct_hook)
  183. kfree(sk->protinfo.destruct_hook);
  184. atomic_dec(&packet_socks_nr);
  185. #ifdef PACKET_REFCNT_DEBUG
  186. printk(KERN_DEBUG "PACKET socket %p is free, %d are aliven", sk, atomic_read(&packet_socks_nr));
  187. #endif
  188. MOD_DEC_USE_COUNT;
  189. }
  190. extern struct proto_ops packet_ops;
  191. #ifdef CONFIG_SOCK_PACKET
  192. extern struct proto_ops packet_ops_spkt;
  193. static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,  struct packet_type *pt)
  194. {
  195. struct sock *sk;
  196. struct sockaddr_pkt *spkt;
  197. /*
  198.  * When we registered the protocol we saved the socket in the data
  199.  * field for just this event.
  200.  */
  201. sk = (struct sock *) pt->data;
  202. /*
  203.  * Yank back the headers [hope the device set this
  204.  * right or kerboom...]
  205.  *
  206.  * Incoming packets have ll header pulled,
  207.  * push it back.
  208.  *
  209.  * For outgoing ones skb->data == skb->mac.raw
  210.  * so that this procedure is noop.
  211.  */
  212. if (skb->pkt_type == PACKET_LOOPBACK)
  213. goto out;
  214. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  215. goto oom;
  216. spkt = (struct sockaddr_pkt*)skb->cb;
  217. skb_push(skb, skb->data-skb->mac.raw);
  218. /*
  219.  * The SOCK_PACKET socket receives _all_ frames.
  220.  */
  221. spkt->spkt_family = dev->type;
  222. strncpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
  223. spkt->spkt_protocol = skb->protocol;
  224. /*
  225.  * Charge the memory to the socket. This is done specifically
  226.  * to prevent sockets using all the memory up.
  227.  */
  228. if (sock_queue_rcv_skb(sk,skb) == 0)
  229. return 0;
  230. out:
  231. kfree_skb(skb);
  232. oom:
  233. return 0;
  234. }
  235. /*
  236.  * Output a raw packet to a device layer. This bypasses all the other
  237.  * protocol layers and you must therefore supply it with a complete frame
  238.  */
  239.  
  240. static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg, int len,
  241.        struct scm_cookie *scm)
  242. {
  243. struct sock *sk = sock->sk;
  244. struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
  245. struct sk_buff *skb;
  246. struct net_device *dev;
  247. unsigned short proto=0;
  248. int err;
  249. /*
  250.  * Get and verify the address. 
  251.  */
  252. if (saddr)
  253. {
  254. if (msg->msg_namelen < sizeof(struct sockaddr))
  255. return(-EINVAL);
  256. if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
  257. proto=saddr->spkt_protocol;
  258. }
  259. else
  260. return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
  261. /*
  262.  * Find the device first to size check it 
  263.  */
  264. saddr->spkt_device[13] = 0;
  265. dev = dev_get_by_name(saddr->spkt_device);
  266. err = -ENODEV;
  267. if (dev == NULL)
  268. goto out_unlock;
  269. /*
  270.  * You may not queue a frame bigger than the mtu. This is the lowest level
  271.  * raw protocol and you must do your own fragmentation at this level.
  272.  */
  273.  
  274. err = -EMSGSIZE;
  275.   if(len>dev->mtu+dev->hard_header_len)
  276. goto out_unlock;
  277. err = -ENOBUFS;
  278. skb = sock_wmalloc(sk, len+dev->hard_header_len+15, 0, GFP_KERNEL);
  279. /*
  280.  * If the write buffer is full, then tough. At this level the user gets to
  281.  * deal with the problem - do your own algorithmic backoffs. That's far
  282.  * more flexible.
  283.  */
  284.  
  285. if (skb == NULL) 
  286. goto out_unlock;
  287. /*
  288.  * Fill it in 
  289.  */
  290.  
  291. /* FIXME: Save some space for broken drivers that write a
  292.  * hard header at transmission time by themselves. PPP is the
  293.  * notable one here. This should really be fixed at the driver level.
  294.  */
  295. skb_reserve(skb,(dev->hard_header_len+15)&~15);
  296. skb->nh.raw = skb->data;
  297. /* Try to align data part correctly */
  298. if (dev->hard_header) {
  299. skb->data -= dev->hard_header_len;
  300. skb->tail -= dev->hard_header_len;
  301. if (len < dev->hard_header_len)
  302. skb->nh.raw = skb->data;
  303. }
  304. /* Returns -EFAULT on error */
  305. err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
  306. skb->protocol = proto;
  307. skb->dev = dev;
  308. skb->priority = sk->priority;
  309. if (err)
  310. goto out_free;
  311. err = -ENETDOWN;
  312. if (!(dev->flags & IFF_UP))
  313. goto out_free;
  314. /*
  315.  * Now send it
  316.  */
  317. dev_queue_xmit(skb);
  318. dev_put(dev);
  319. return(len);
  320. out_free:
  321. kfree_skb(skb);
  322. out_unlock:
  323. if (dev)
  324. dev_put(dev);
  325. return err;
  326. }
  327. #endif
  328. /*
  329.    This function makes lazy skb cloning in hope that most of packets
  330.    are discarded by BPF.
  331.    Note tricky part: we DO mangle shared skb! skb->data, skb->len
  332.    and skb->cb are mangled. It works because (and until) packets
  333.    falling here are owned by current CPU. Output packets are cloned
  334.    by dev_queue_xmit_nit(), input packets are processed by net_bh
  335.    sequencially, so that if we return skb to original state on exit,
  336.    we will not harm anyone.
  337.  */
  338. static int packet_rcv(struct sk_buff *skb, struct net_device *dev,  struct packet_type *pt)
  339. {
  340. struct sock *sk;
  341. struct sockaddr_ll *sll;
  342. struct packet_opt *po;
  343. u8 * skb_head = skb->data;
  344. int skb_len = skb->len;
  345. #ifdef CONFIG_FILTER
  346. unsigned snaplen;
  347. #endif
  348. if (skb->pkt_type == PACKET_LOOPBACK)
  349. goto drop;
  350. sk = (struct sock *) pt->data;
  351. po = sk->protinfo.af_packet;
  352. skb->dev = dev;
  353. if (dev->hard_header) {
  354. /* The device has an explicit notion of ll header,
  355.    exported to higher levels.
  356.    Otherwise, the device hides datails of it frame
  357.    structure, so that corresponding packet head
  358.    never delivered to user.
  359.  */
  360. if (sk->type != SOCK_DGRAM)
  361. skb_push(skb, skb->data - skb->mac.raw);
  362. else if (skb->pkt_type == PACKET_OUTGOING) {
  363. /* Special case: outgoing packets have ll header at head */
  364. skb_pull(skb, skb->nh.raw - skb->data);
  365. }
  366. }
  367. #ifdef CONFIG_FILTER
  368. snaplen = skb->len;
  369. if (sk->filter) {
  370. unsigned res = snaplen;
  371. struct sk_filter *filter;
  372. bh_lock_sock(sk);
  373. if ((filter = sk->filter) != NULL)
  374. res = sk_run_filter(skb, sk->filter->insns, sk->filter->len);
  375. bh_unlock_sock(sk);
  376. if (res == 0)
  377. goto drop_n_restore;
  378. if (snaplen > res)
  379. snaplen = res;
  380. }
  381. #endif /* CONFIG_FILTER */
  382. if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf)
  383. goto drop_n_acct;
  384. if (skb_shared(skb)) {
  385. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  386. if (nskb == NULL)
  387. goto drop_n_acct;
  388. if (skb_head != skb->data) {
  389. skb->data = skb_head;
  390. skb->len = skb_len;
  391. }
  392. kfree_skb(skb);
  393. skb = nskb;
  394. }
  395. sll = (struct sockaddr_ll*)skb->cb;
  396. sll->sll_family = AF_PACKET;
  397. sll->sll_hatype = dev->type;
  398. sll->sll_protocol = skb->protocol;
  399. sll->sll_pkttype = skb->pkt_type;
  400. sll->sll_ifindex = dev->ifindex;
  401. sll->sll_halen = 0;
  402. if (dev->hard_header_parse)
  403. sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
  404. #ifdef CONFIG_FILTER
  405. if (pskb_trim(skb, snaplen))
  406. goto drop_n_acct;
  407. #endif
  408. skb_set_owner_r(skb, sk);
  409. skb->dev = NULL;
  410. spin_lock(&sk->receive_queue.lock);
  411. po->stats.tp_packets++;
  412. __skb_queue_tail(&sk->receive_queue, skb);
  413. spin_unlock(&sk->receive_queue.lock);
  414. sk->data_ready(sk,skb->len);
  415. return 0;
  416. drop_n_acct:
  417. spin_lock(&sk->receive_queue.lock);
  418. po->stats.tp_drops++;
  419. spin_unlock(&sk->receive_queue.lock);
  420. #ifdef CONFIG_FILTER
  421. drop_n_restore:
  422. #endif
  423. if (skb_head != skb->data && skb_shared(skb)) {
  424. skb->data = skb_head;
  425. skb->len = skb_len;
  426. }
  427. drop:
  428. kfree_skb(skb);
  429. return 0;
  430. }
  431. #ifdef CONFIG_PACKET_MMAP
  432. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,  struct packet_type *pt)
  433. {
  434. struct sock *sk;
  435. struct packet_opt *po;
  436. struct sockaddr_ll *sll;
  437. struct tpacket_hdr *h;
  438. u8 * skb_head = skb->data;
  439. int skb_len = skb->len;
  440. unsigned snaplen;
  441. unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
  442. unsigned short macoff, netoff;
  443. struct sk_buff *copy_skb = NULL;
  444. if (skb->pkt_type == PACKET_LOOPBACK)
  445. goto drop;
  446. sk = (struct sock *) pt->data;
  447. po = sk->protinfo.af_packet;
  448. if (dev->hard_header) {
  449. if (sk->type != SOCK_DGRAM)
  450. skb_push(skb, skb->data - skb->mac.raw);
  451. else if (skb->pkt_type == PACKET_OUTGOING) {
  452. /* Special case: outgoing packets have ll header at head */
  453. skb_pull(skb, skb->nh.raw - skb->data);
  454. if (skb->ip_summed == CHECKSUM_HW)
  455. status |= TP_STATUS_CSUMNOTREADY;
  456. }
  457. }
  458. snaplen = skb->len;
  459. #ifdef CONFIG_FILTER
  460. if (sk->filter) {
  461. unsigned res = snaplen;
  462. struct sk_filter *filter;
  463. bh_lock_sock(sk);
  464. if ((filter = sk->filter) != NULL)
  465. res = sk_run_filter(skb, sk->filter->insns, sk->filter->len);
  466. bh_unlock_sock(sk);
  467. if (res == 0)
  468. goto drop_n_restore;
  469. if (snaplen > res)
  470. snaplen = res;
  471. }
  472. #endif
  473. if (sk->type == SOCK_DGRAM) {
  474. macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
  475. } else {
  476. unsigned maclen = skb->nh.raw - skb->data;
  477. netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
  478. macoff = netoff - maclen;
  479. }
  480. if (macoff + snaplen > po->frame_size) {
  481. if (po->copy_thresh &&
  482.     atomic_read(&sk->rmem_alloc) + skb->truesize < (unsigned)sk->rcvbuf) {
  483. if (skb_shared(skb)) {
  484. copy_skb = skb_clone(skb, GFP_ATOMIC);
  485. } else {
  486. copy_skb = skb_get(skb);
  487. skb_head = skb->data;
  488. }
  489. if (copy_skb)
  490. skb_set_owner_r(copy_skb, sk);
  491. }
  492. snaplen = po->frame_size - macoff;
  493. if ((int)snaplen < 0)
  494. snaplen = 0;
  495. }
  496. if (snaplen > skb->len-skb->data_len)
  497. snaplen = skb->len-skb->data_len;
  498. spin_lock(&sk->receive_queue.lock);
  499. h = po->iovec[po->head];
  500. if (h->tp_status)
  501. goto ring_is_full;
  502. po->head = po->head != po->iovmax ? po->head+1 : 0;
  503. po->stats.tp_packets++;
  504. if (copy_skb) {
  505. status |= TP_STATUS_COPY;
  506. __skb_queue_tail(&sk->receive_queue, copy_skb);
  507. }
  508. if (!po->stats.tp_drops)
  509. status &= ~TP_STATUS_LOSING;
  510. spin_unlock(&sk->receive_queue.lock);
  511. memcpy((u8*)h + macoff, skb->data, snaplen);
  512. h->tp_len = skb->len;
  513. h->tp_snaplen = snaplen;
  514. h->tp_mac = macoff;
  515. h->tp_net = netoff;
  516. h->tp_sec = skb->stamp.tv_sec;
  517. h->tp_usec = skb->stamp.tv_usec;
  518. sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
  519. sll->sll_halen = 0;
  520. if (dev->hard_header_parse)
  521. sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
  522. sll->sll_family = AF_PACKET;
  523. sll->sll_hatype = dev->type;
  524. sll->sll_protocol = skb->protocol;
  525. sll->sll_pkttype = skb->pkt_type;
  526. sll->sll_ifindex = dev->ifindex;
  527. h->tp_status = status;
  528. mb();
  529. {
  530. struct page *p_start, *p_end;
  531. u8 *h_end = (u8 *)h + macoff + snaplen - 1;
  532. p_start = virt_to_page(h);
  533. p_end = virt_to_page(h_end);
  534. while (p_start <= p_end) {
  535. flush_dcache_page(p_start);
  536. p_start++;
  537. }
  538. }
  539. sk->data_ready(sk, 0);
  540. drop_n_restore:
  541. if (skb_head != skb->data && skb_shared(skb)) {
  542. skb->data = skb_head;
  543. skb->len = skb_len;
  544. }
  545. drop:
  546.         kfree_skb(skb);
  547. return 0;
  548. ring_is_full:
  549. po->stats.tp_drops++;
  550. spin_unlock(&sk->receive_queue.lock);
  551. sk->data_ready(sk, 0);
  552. if (copy_skb)
  553. kfree_skb(copy_skb);
  554. goto drop_n_restore;
  555. }
  556. #endif
  557. static int packet_sendmsg(struct socket *sock, struct msghdr *msg, int len,
  558.   struct scm_cookie *scm)
  559. {
  560. struct sock *sk = sock->sk;
  561. struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
  562. struct sk_buff *skb;
  563. struct net_device *dev;
  564. unsigned short proto;
  565. unsigned char *addr;
  566. int ifindex, err, reserve = 0;
  567. /*
  568.  * Get and verify the address. 
  569.  */
  570.  
  571. if (saddr == NULL) {
  572. ifindex = sk->protinfo.af_packet->ifindex;
  573. proto = sk->num;
  574. addr = NULL;
  575. } else {
  576. err = -EINVAL;
  577. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  578. goto out;
  579. ifindex = saddr->sll_ifindex;
  580. proto = saddr->sll_protocol;
  581. addr = saddr->sll_addr;
  582. }
  583. dev = dev_get_by_index(ifindex);
  584. err = -ENXIO;
  585. if (dev == NULL)
  586. goto out_unlock;
  587. if (sock->type == SOCK_RAW)
  588. reserve = dev->hard_header_len;
  589. err = -EMSGSIZE;
  590. if (len > dev->mtu+reserve)
  591. goto out_unlock;
  592. skb = sock_alloc_send_skb(sk, len+dev->hard_header_len+15, 
  593. msg->msg_flags & MSG_DONTWAIT, &err);
  594. if (skb==NULL)
  595. goto out_unlock;
  596. skb_reserve(skb, (dev->hard_header_len+15)&~15);
  597. skb->nh.raw = skb->data;
  598. if (dev->hard_header) {
  599. int res;
  600. err = -EINVAL;
  601. res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
  602. if (sock->type != SOCK_DGRAM) {
  603. skb->tail = skb->data;
  604. skb->len = 0;
  605. } else if (res < 0)
  606. goto out_free;
  607. }
  608. /* Returns -EFAULT on error */
  609. err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
  610. if (err)
  611. goto out_free;
  612. skb->protocol = proto;
  613. skb->dev = dev;
  614. skb->priority = sk->priority;
  615. err = -ENETDOWN;
  616. if (!(dev->flags & IFF_UP))
  617. goto out_free;
  618. /*
  619.  * Now send it
  620.  */
  621. err = dev_queue_xmit(skb);
  622. if (err > 0 && (err = net_xmit_errno(err)) != 0)
  623. goto out_unlock;
  624. dev_put(dev);
  625. return(len);
  626. out_free:
  627. kfree_skb(skb);
  628. out_unlock:
  629. if (dev)
  630. dev_put(dev);
  631. out:
  632. return err;
  633. }
  634. /*
  635.  * Close a PACKET socket. This is fairly simple. We immediately go
  636.  * to 'closed' state and remove our protocol entry in the device list.
  637.  */
  638. static int packet_release(struct socket *sock)
  639. {
  640. struct sock *sk = sock->sk;
  641. struct sock **skp;
  642. if (!sk)
  643. return 0;
  644. write_lock_bh(&packet_sklist_lock);
  645. for (skp = &packet_sklist; *skp; skp = &(*skp)->next) {
  646. if (*skp == sk) {
  647. *skp = sk->next;
  648. __sock_put(sk);
  649. break;
  650. }
  651. }
  652. write_unlock_bh(&packet_sklist_lock);
  653. /*
  654.  * Unhook packet receive handler.
  655.  */
  656. if (sk->protinfo.af_packet->running) {
  657. /*
  658.  * Remove the protocol hook
  659.  */
  660. dev_remove_pack(&sk->protinfo.af_packet->prot_hook);
  661. sk->protinfo.af_packet->running = 0;
  662. __sock_put(sk);
  663. }
  664. #ifdef CONFIG_PACKET_MULTICAST
  665. packet_flush_mclist(sk);
  666. #endif
  667. #ifdef CONFIG_PACKET_MMAP
  668. if (sk->protinfo.af_packet->pg_vec) {
  669. struct tpacket_req req;
  670. memset(&req, 0, sizeof(req));
  671. packet_set_ring(sk, &req, 1);
  672. }
  673. #endif
  674. /*
  675.  * Now the socket is dead. No more input will appear.
  676.  */
  677. sock_orphan(sk);
  678. sock->sk = NULL;
  679. /* Purge queues */
  680. skb_queue_purge(&sk->receive_queue);
  681. sock_put(sk);
  682. return 0;
  683. }
  684. /*
  685.  * Attach a packet hook.
  686.  */
  687. static int packet_do_bind(struct sock *sk, struct net_device *dev, int protocol)
  688. {
  689. /*
  690.  * Detach an existing hook if present.
  691.  */
  692. lock_sock(sk);
  693. spin_lock(&sk->protinfo.af_packet->bind_lock);
  694. if (sk->protinfo.af_packet->running) {
  695. dev_remove_pack(&sk->protinfo.af_packet->prot_hook);
  696. __sock_put(sk);
  697. sk->protinfo.af_packet->running = 0;
  698. }
  699. sk->num = protocol;
  700. sk->protinfo.af_packet->prot_hook.type = protocol;
  701. sk->protinfo.af_packet->prot_hook.dev = dev;
  702. sk->protinfo.af_packet->ifindex = dev ? dev->ifindex : 0;
  703. if (protocol == 0)
  704. goto out_unlock;
  705. if (dev) {
  706. if (dev->flags&IFF_UP) {
  707. dev_add_pack(&sk->protinfo.af_packet->prot_hook);
  708. sock_hold(sk);
  709. sk->protinfo.af_packet->running = 1;
  710. } else {
  711. sk->err = ENETDOWN;
  712. if (!sk->dead)
  713. sk->error_report(sk);
  714. }
  715. } else {
  716. dev_add_pack(&sk->protinfo.af_packet->prot_hook);
  717. sock_hold(sk);
  718. sk->protinfo.af_packet->running = 1;
  719. }
  720. out_unlock:
  721. spin_unlock(&sk->protinfo.af_packet->bind_lock);
  722. release_sock(sk);
  723. return 0;
  724. }
  725. /*
  726.  * Bind a packet socket to a device
  727.  */
  728. #ifdef CONFIG_SOCK_PACKET
  729. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  730. {
  731. struct sock *sk=sock->sk;
  732. char name[15];
  733. struct net_device *dev;
  734. int err = -ENODEV;
  735. /*
  736.  * Check legality
  737.  */
  738.  
  739. if(addr_len!=sizeof(struct sockaddr))
  740. return -EINVAL;
  741. strncpy(name,uaddr->sa_data,14);
  742. name[14]=0;
  743. dev = dev_get_by_name(name);
  744. if (dev) {
  745. err = packet_do_bind(sk, dev, sk->num);
  746. dev_put(dev);
  747. }
  748. return err;
  749. }
  750. #endif
  751. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  752. {
  753. struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
  754. struct sock *sk=sock->sk;
  755. struct net_device *dev = NULL;
  756. int err;
  757. /*
  758.  * Check legality
  759.  */
  760.  
  761. if (addr_len < sizeof(struct sockaddr_ll))
  762. return -EINVAL;
  763. if (sll->sll_family != AF_PACKET)
  764. return -EINVAL;
  765. if (sll->sll_ifindex) {
  766. err = -ENODEV;
  767. dev = dev_get_by_index(sll->sll_ifindex);
  768. if (dev == NULL)
  769. goto out;
  770. }
  771. err = packet_do_bind(sk, dev, sll->sll_protocol ? : sk->num);
  772. if (dev)
  773. dev_put(dev);
  774. out:
  775. return err;
  776. }
  777. /*
  778.  * Create a packet of type SOCK_PACKET. 
  779.  */
  780. static int packet_create(struct socket *sock, int protocol)
  781. {
  782. struct sock *sk;
  783. int err;
  784. if (!capable(CAP_NET_RAW))
  785. return -EPERM;
  786. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW
  787. #ifdef CONFIG_SOCK_PACKET
  788.     && sock->type != SOCK_PACKET
  789. #endif
  790.     )
  791. return -ESOCKTNOSUPPORT;
  792. sock->state = SS_UNCONNECTED;
  793. MOD_INC_USE_COUNT;
  794. err = -ENOBUFS;
  795. sk = sk_alloc(PF_PACKET, GFP_KERNEL, 1);
  796. if (sk == NULL)
  797. goto out;
  798. sock->ops = &packet_ops;
  799. #ifdef CONFIG_SOCK_PACKET
  800. if (sock->type == SOCK_PACKET)
  801. sock->ops = &packet_ops_spkt;
  802. #endif
  803. sock_init_data(sock,sk);
  804. sk->protinfo.af_packet = kmalloc(sizeof(struct packet_opt), GFP_KERNEL);
  805. if (sk->protinfo.af_packet == NULL)
  806. goto out_free;
  807. memset(sk->protinfo.af_packet, 0, sizeof(struct packet_opt));
  808. sk->family = PF_PACKET;
  809. sk->num = protocol;
  810. sk->destruct = packet_sock_destruct;
  811. atomic_inc(&packet_socks_nr);
  812. /*
  813.  * Attach a protocol block
  814.  */
  815. spin_lock_init(&sk->protinfo.af_packet->bind_lock);
  816. sk->protinfo.af_packet->prot_hook.func = packet_rcv;
  817. #ifdef CONFIG_SOCK_PACKET
  818. if (sock->type == SOCK_PACKET)
  819. sk->protinfo.af_packet->prot_hook.func = packet_rcv_spkt;
  820. #endif
  821. sk->protinfo.af_packet->prot_hook.data = (void *)sk;
  822. if (protocol) {
  823. sk->protinfo.af_packet->prot_hook.type = protocol;
  824. dev_add_pack(&sk->protinfo.af_packet->prot_hook);
  825. sock_hold(sk);
  826. sk->protinfo.af_packet->running = 1;
  827. }
  828. write_lock_bh(&packet_sklist_lock);
  829. sk->next = packet_sklist;
  830. packet_sklist = sk;
  831. sock_hold(sk);
  832. write_unlock_bh(&packet_sklist_lock);
  833. return(0);
  834. out_free:
  835. sk_free(sk);
  836. out:
  837. MOD_DEC_USE_COUNT;
  838. return err;
  839. }
  840. /*
  841.  * Pull a packet from our receive queue and hand it to the user.
  842.  * If necessary we block.
  843.  */
  844. static int packet_recvmsg(struct socket *sock, struct msghdr *msg, int len,
  845.   int flags, struct scm_cookie *scm)
  846. {
  847. struct sock *sk = sock->sk;
  848. struct sk_buff *skb;
  849. int copied, err;
  850. err = -EINVAL;
  851. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC))
  852. goto out;
  853. #if 0
  854. /* What error should we return now? EUNATTACH? */
  855. if (sk->protinfo.af_packet->ifindex < 0)
  856. return -ENODEV;
  857. #endif
  858. /*
  859.  * If the address length field is there to be filled in, we fill
  860.  * it in now.
  861.  */
  862. if (sock->type == SOCK_PACKET)
  863. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  864. else
  865. msg->msg_namelen = sizeof(struct sockaddr_ll);
  866. /*
  867.  * Call the generic datagram receiver. This handles all sorts
  868.  * of horrible races and re-entrancy so we can forget about it
  869.  * in the protocol layers.
  870.  *
  871.  * Now it will return ENETDOWN, if device have just gone down,
  872.  * but then it will block.
  873.  */
  874. skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
  875. /*
  876.  * An error occurred so return it. Because skb_recv_datagram() 
  877.  * handles the blocking we don't see and worry about blocking
  878.  * retries.
  879.  */
  880. if(skb==NULL)
  881. goto out;
  882. /*
  883.  * You lose any data beyond the buffer you gave. If it worries a
  884.  * user program they can ask the device for its MTU anyway.
  885.  */
  886. copied = skb->len;
  887. if (copied > len)
  888. {
  889. copied=len;
  890. msg->msg_flags|=MSG_TRUNC;
  891. }
  892. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  893. if (err)
  894. goto out_free;
  895. sock_recv_timestamp(msg, sk, skb);
  896. if (msg->msg_name)
  897. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  898. /*
  899.  * Free or return the buffer as appropriate. Again this
  900.  * hides all the races and re-entrancy issues from us.
  901.  */
  902. err = (flags&MSG_TRUNC) ? skb->len : copied;
  903. out_free:
  904. skb_free_datagram(sk, skb);
  905. out:
  906. return err;
  907. }
  908. #ifdef CONFIG_SOCK_PACKET
  909. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  910.        int *uaddr_len, int peer)
  911. {
  912. struct net_device *dev;
  913. struct sock *sk = sock->sk;
  914. if (peer)
  915. return -EOPNOTSUPP;
  916. uaddr->sa_family = AF_PACKET;
  917. dev = dev_get_by_index(sk->protinfo.af_packet->ifindex);
  918. if (dev) {
  919. strncpy(uaddr->sa_data, dev->name, 15);
  920. dev_put(dev);
  921. } else
  922. memset(uaddr->sa_data, 0, 14);
  923. *uaddr_len = sizeof(*uaddr);
  924. return 0;
  925. }
  926. #endif
  927. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  928.   int *uaddr_len, int peer)
  929. {
  930. struct net_device *dev;
  931. struct sock *sk = sock->sk;
  932. struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
  933. if (peer)
  934. return -EOPNOTSUPP;
  935. sll->sll_family = AF_PACKET;
  936. sll->sll_ifindex = sk->protinfo.af_packet->ifindex;
  937. sll->sll_protocol = sk->num;
  938. dev = dev_get_by_index(sk->protinfo.af_packet->ifindex);
  939. if (dev) {
  940. sll->sll_hatype = dev->type;
  941. sll->sll_halen = dev->addr_len;
  942. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  943. dev_put(dev);
  944. } else {
  945. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  946. sll->sll_halen = 0;
  947. }
  948. *uaddr_len = sizeof(*sll);
  949. return 0;
  950. }
  951. #ifdef CONFIG_PACKET_MULTICAST
  952. static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
  953. {
  954. switch (i->type) {
  955. case PACKET_MR_MULTICAST:
  956. if (what > 0)
  957. dev_mc_add(dev, i->addr, i->alen, 0);
  958. else
  959. dev_mc_delete(dev, i->addr, i->alen, 0);
  960. break;
  961. case PACKET_MR_PROMISC:
  962. dev_set_promiscuity(dev, what);
  963. break;
  964. case PACKET_MR_ALLMULTI:
  965. dev_set_allmulti(dev, what);
  966. break;
  967. default:;
  968. }
  969. }
  970. static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
  971. {
  972. for ( ; i; i=i->next) {
  973. if (i->ifindex == dev->ifindex)
  974. packet_dev_mc(dev, i, what);
  975. }
  976. }
  977. static int packet_mc_add(struct sock *sk, struct packet_mreq *mreq)
  978. {
  979. struct packet_mclist *ml, *i;
  980. struct net_device *dev;
  981. int err;
  982. rtnl_lock();
  983. err = -ENODEV;
  984. dev = __dev_get_by_index(mreq->mr_ifindex);
  985. if (!dev)
  986. goto done;
  987. err = -EINVAL;
  988. if (mreq->mr_alen > dev->addr_len)
  989. goto done;
  990. err = -ENOBUFS;
  991. i = (struct packet_mclist *)kmalloc(sizeof(*i), GFP_KERNEL);
  992. if (i == NULL)
  993. goto done;
  994. err = 0;
  995. for (ml=sk->protinfo.af_packet->mclist; ml; ml=ml->next) {
  996. if (ml->ifindex == mreq->mr_ifindex &&
  997.     ml->type == mreq->mr_type &&
  998.     ml->alen == mreq->mr_alen &&
  999.     memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1000. ml->count++;
  1001. /* Free the new element ... */
  1002. kfree(i);
  1003. goto done;
  1004. }
  1005. }
  1006. i->type = mreq->mr_type;
  1007. i->ifindex = mreq->mr_ifindex;
  1008. i->alen = mreq->mr_alen;
  1009. memcpy(i->addr, mreq->mr_address, i->alen);
  1010. i->count = 1;
  1011. i->next = sk->protinfo.af_packet->mclist;
  1012. sk->protinfo.af_packet->mclist = i;
  1013. packet_dev_mc(dev, i, +1);
  1014. done:
  1015. rtnl_unlock();
  1016. return err;
  1017. }
  1018. static int packet_mc_drop(struct sock *sk, struct packet_mreq *mreq)
  1019. {
  1020. struct packet_mclist *ml, **mlp;
  1021. rtnl_lock();
  1022. for (mlp=&sk->protinfo.af_packet->mclist; (ml=*mlp)!=NULL; mlp=&ml->next) {
  1023. if (ml->ifindex == mreq->mr_ifindex &&
  1024.     ml->type == mreq->mr_type &&
  1025.     ml->alen == mreq->mr_alen &&
  1026.     memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1027. if (--ml->count == 0) {
  1028. struct net_device *dev;
  1029. *mlp = ml->next;
  1030. dev = dev_get_by_index(ml->ifindex);
  1031. if (dev) {
  1032. packet_dev_mc(dev, ml, -1);
  1033. dev_put(dev);
  1034. }
  1035. kfree(ml);
  1036. }
  1037. rtnl_unlock();
  1038. return 0;
  1039. }
  1040. }
  1041. rtnl_unlock();
  1042. return -EADDRNOTAVAIL;
  1043. }
  1044. static void packet_flush_mclist(struct sock *sk)
  1045. {
  1046. struct packet_mclist *ml;
  1047. if (sk->protinfo.af_packet->mclist == NULL)
  1048. return;
  1049. rtnl_lock();
  1050. while ((ml=sk->protinfo.af_packet->mclist) != NULL) {
  1051. struct net_device *dev;
  1052. sk->protinfo.af_packet->mclist = ml->next;
  1053. if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
  1054. packet_dev_mc(dev, ml, -1);
  1055. dev_put(dev);
  1056. }
  1057. kfree(ml);
  1058. }
  1059. rtnl_unlock();
  1060. }
  1061. #endif
  1062. static int
  1063. packet_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
  1064. {
  1065. struct sock *sk = sock->sk;
  1066. int ret;
  1067. if (level != SOL_PACKET)
  1068. return -ENOPROTOOPT;
  1069. switch(optname) {
  1070. #ifdef CONFIG_PACKET_MULTICAST
  1071. case PACKET_ADD_MEMBERSHIP:
  1072. case PACKET_DROP_MEMBERSHIP:
  1073. {
  1074. struct packet_mreq mreq;
  1075. if (optlen<sizeof(mreq))
  1076. return -EINVAL;
  1077. if (copy_from_user(&mreq,optval,sizeof(mreq)))
  1078. return -EFAULT;
  1079. if (optname == PACKET_ADD_MEMBERSHIP)
  1080. ret = packet_mc_add(sk, &mreq);
  1081. else
  1082. ret = packet_mc_drop(sk, &mreq);
  1083. return ret;
  1084. }
  1085. #endif
  1086. #ifdef CONFIG_PACKET_MMAP
  1087. case PACKET_RX_RING:
  1088. {
  1089. struct tpacket_req req;
  1090. if (optlen<sizeof(req))
  1091. return -EINVAL;
  1092. if (copy_from_user(&req,optval,sizeof(req)))
  1093. return -EFAULT;
  1094. return packet_set_ring(sk, &req, 0);
  1095. }
  1096. case PACKET_COPY_THRESH:
  1097. {
  1098. int val;
  1099. if (optlen!=sizeof(val))
  1100. return -EINVAL;
  1101. if (copy_from_user(&val,optval,sizeof(val)))
  1102. return -EFAULT;
  1103. sk->protinfo.af_packet->copy_thresh = val;
  1104. return 0;
  1105. }
  1106. #endif
  1107. default:
  1108. return -ENOPROTOOPT;
  1109. }
  1110. }
  1111. int packet_getsockopt(struct socket *sock, int level, int optname,
  1112.       char *optval, int *optlen)
  1113. {
  1114. int len;
  1115. struct sock *sk = sock->sk;
  1116. if (level != SOL_PACKET)
  1117. return -ENOPROTOOPT;
  1118.    if (get_user(len,optlen))
  1119.    return -EFAULT;
  1120. if (len < 0)
  1121. return -EINVAL;
  1122. switch(optname) {
  1123. case PACKET_STATISTICS:
  1124. {
  1125. struct tpacket_stats st;
  1126. if (len > sizeof(struct tpacket_stats))
  1127. len = sizeof(struct tpacket_stats);
  1128. spin_lock_bh(&sk->receive_queue.lock);
  1129. st = sk->protinfo.af_packet->stats;
  1130. memset(&sk->protinfo.af_packet->stats, 0, sizeof(st));
  1131. spin_unlock_bh(&sk->receive_queue.lock);
  1132. st.tp_packets += st.tp_drops;
  1133. if (copy_to_user(optval, &st, len))
  1134. return -EFAULT;
  1135. break;
  1136. }
  1137. default:
  1138. return -ENOPROTOOPT;
  1139. }
  1140.    if (put_user(len, optlen))
  1141.    return -EFAULT;
  1142.    return 0;
  1143. }
  1144. static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
  1145. {
  1146. struct sock *sk;
  1147. struct packet_opt *po;
  1148. struct net_device *dev = (struct net_device*)data;
  1149. read_lock(&packet_sklist_lock);
  1150. for (sk = packet_sklist; sk; sk = sk->next) {
  1151. po = sk->protinfo.af_packet;
  1152. switch (msg) {
  1153. case NETDEV_DOWN:
  1154. case NETDEV_UNREGISTER:
  1155. if (dev->ifindex == po->ifindex) {
  1156. spin_lock(&po->bind_lock);
  1157. if (po->running) {
  1158. dev_remove_pack(&po->prot_hook);
  1159. __sock_put(sk);
  1160. po->running = 0;
  1161. sk->err = ENETDOWN;
  1162. if (!sk->dead)
  1163. sk->error_report(sk);
  1164. }
  1165. if (msg == NETDEV_UNREGISTER) {
  1166. po->ifindex = -1;
  1167. po->prot_hook.dev = NULL;
  1168. }
  1169. spin_unlock(&po->bind_lock);
  1170. }
  1171. #ifdef CONFIG_PACKET_MULTICAST
  1172. if (po->mclist)
  1173. packet_dev_mclist(dev, po->mclist, -1);
  1174. #endif
  1175. break;
  1176. case NETDEV_UP:
  1177. spin_lock(&po->bind_lock);
  1178. if (dev->ifindex == po->ifindex && sk->num && po->running==0) {
  1179. dev_add_pack(&po->prot_hook);
  1180. sock_hold(sk);
  1181. po->running = 1;
  1182. }
  1183. spin_unlock(&po->bind_lock);
  1184. #ifdef CONFIG_PACKET_MULTICAST
  1185. if (po->mclist)
  1186. packet_dev_mclist(dev, po->mclist, +1);
  1187. #endif
  1188. break;
  1189. }
  1190. }
  1191. read_unlock(&packet_sklist_lock);
  1192. return NOTIFY_DONE;
  1193. }
  1194. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  1195. unsigned long arg)
  1196. {
  1197. struct sock *sk = sock->sk;
  1198. switch(cmd) 
  1199. {
  1200. case SIOCOUTQ:
  1201. {
  1202. int amount = atomic_read(&sk->wmem_alloc);
  1203. return put_user(amount, (int *)arg);
  1204. }
  1205. case SIOCINQ:
  1206. {
  1207. struct sk_buff *skb;
  1208. int amount = 0;
  1209. spin_lock_bh(&sk->receive_queue.lock);
  1210. skb = skb_peek(&sk->receive_queue);
  1211. if (skb)
  1212. amount = skb->len;
  1213. spin_unlock_bh(&sk->receive_queue.lock);
  1214. return put_user(amount, (int *)arg);
  1215. }
  1216. case FIOSETOWN:
  1217. case SIOCSPGRP: {
  1218. int pid;
  1219. if (get_user(pid, (int *) arg))
  1220. return -EFAULT; 
  1221. if (current->pid != pid && current->pgrp != -pid && 
  1222.     !capable(CAP_NET_ADMIN))
  1223. return -EPERM;
  1224. sk->proc = pid;
  1225. break;
  1226. }
  1227. case FIOGETOWN:
  1228. case SIOCGPGRP:
  1229. return put_user(sk->proc, (int *)arg);
  1230. case SIOCGSTAMP:
  1231. if(sk->stamp.tv_sec==0)
  1232. return -ENOENT;
  1233. if (copy_to_user((void *)arg, &sk->stamp,
  1234.  sizeof(struct timeval)))
  1235. return -EFAULT;
  1236. break;
  1237. case SIOCGIFFLAGS:
  1238. #ifndef CONFIG_INET
  1239. case SIOCSIFFLAGS:
  1240. #endif
  1241. case SIOCGIFCONF:
  1242. case SIOCGIFMETRIC:
  1243. case SIOCSIFMETRIC:
  1244. case SIOCGIFMEM:
  1245. case SIOCSIFMEM:
  1246. case SIOCGIFMTU:
  1247. case SIOCSIFMTU:
  1248. case SIOCSIFLINK:
  1249. case SIOCGIFHWADDR:
  1250. case SIOCSIFHWADDR:
  1251. case SIOCSIFMAP:
  1252. case SIOCGIFMAP:
  1253. case SIOCSIFSLAVE:
  1254. case SIOCGIFSLAVE:
  1255. case SIOCGIFINDEX:
  1256. case SIOCGIFNAME:
  1257. case SIOCGIFCOUNT:
  1258. case SIOCSIFHWBROADCAST:
  1259. return(dev_ioctl(cmd,(void *) arg));
  1260. case SIOCGIFBR:
  1261. case SIOCSIFBR:
  1262. #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
  1263. #ifdef CONFIG_INET
  1264. #ifdef CONFIG_KMOD
  1265. if (br_ioctl_hook == NULL)
  1266. request_module("bridge");
  1267. #endif
  1268. if (br_ioctl_hook != NULL)
  1269. return br_ioctl_hook(arg);
  1270. #endif
  1271. #endif
  1272. return -ENOPKG;
  1273. case SIOCGIFDIVERT:
  1274. case SIOCSIFDIVERT:
  1275. #ifdef CONFIG_NET_DIVERT
  1276. return divert_ioctl(cmd, (struct divert_cf *) arg);
  1277. #else
  1278. return -ENOPKG;
  1279. #endif /* CONFIG_NET_DIVERT */
  1280. #ifdef CONFIG_INET
  1281. case SIOCADDRT:
  1282. case SIOCDELRT:
  1283. case SIOCDARP:
  1284. case SIOCGARP:
  1285. case SIOCSARP:
  1286. case SIOCGIFADDR:
  1287. case SIOCSIFADDR:
  1288. case SIOCGIFBRDADDR:
  1289. case SIOCSIFBRDADDR:
  1290. case SIOCGIFNETMASK:
  1291. case SIOCSIFNETMASK:
  1292. case SIOCGIFDSTADDR:
  1293. case SIOCSIFDSTADDR:
  1294. case SIOCSIFFLAGS:
  1295. case SIOCADDDLCI:
  1296. case SIOCDELDLCI:
  1297. return inet_dgram_ops.ioctl(sock, cmd, arg);
  1298. #endif
  1299. default:
  1300. if ((cmd >= SIOCDEVPRIVATE) &&
  1301.     (cmd <= (SIOCDEVPRIVATE + 15)))
  1302. return(dev_ioctl(cmd,(void *) arg));
  1303. #ifdef CONFIG_NET_RADIO
  1304. if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
  1305. return(dev_ioctl(cmd,(void *) arg));
  1306. #endif
  1307. return -EOPNOTSUPP;
  1308. }
  1309. return 0;
  1310. }
  1311. #ifndef CONFIG_PACKET_MMAP
  1312. #define packet_mmap sock_no_mmap
  1313. #define packet_poll datagram_poll
  1314. #else
  1315. unsigned int packet_poll(struct file * file, struct socket *sock, poll_table *wait)
  1316. {
  1317. struct sock *sk = sock->sk;
  1318. struct packet_opt *po = sk->protinfo.af_packet;
  1319. unsigned int mask = datagram_poll(file, sock, wait);
  1320. spin_lock_bh(&sk->receive_queue.lock);
  1321. if (po->iovec) {
  1322. unsigned last = po->head ? po->head-1 : po->iovmax;
  1323. if (po->iovec[last]->tp_status)
  1324. mask |= POLLIN | POLLRDNORM;
  1325. }
  1326. spin_unlock_bh(&sk->receive_queue.lock);
  1327. return mask;
  1328. }
  1329. /* Dirty? Well, I still did not learn better way to account
  1330.  * for user mmaps.
  1331.  */
  1332. static void packet_mm_open(struct vm_area_struct *vma)
  1333. {
  1334. struct file *file = vma->vm_file;
  1335. struct inode *inode = file->f_dentry->d_inode;
  1336. struct socket * sock = &inode->u.socket_i;
  1337. struct sock *sk = sock->sk;
  1338. if (sk)
  1339. atomic_inc(&sk->protinfo.af_packet->mapped);
  1340. }
  1341. static void packet_mm_close(struct vm_area_struct *vma)
  1342. {
  1343. struct file *file = vma->vm_file;
  1344. struct inode *inode = file->f_dentry->d_inode;
  1345. struct socket * sock = &inode->u.socket_i;
  1346. struct sock *sk = sock->sk;
  1347. if (sk)
  1348. atomic_dec(&sk->protinfo.af_packet->mapped);
  1349. }
  1350. static struct vm_operations_struct packet_mmap_ops = {
  1351. open: packet_mm_open,
  1352. close: packet_mm_close,
  1353. };
  1354. static void free_pg_vec(unsigned long *pg_vec, unsigned order, unsigned len)
  1355. {
  1356. int i;
  1357. for (i=0; i<len; i++) {
  1358. if (pg_vec[i]) {
  1359. struct page *page, *pend;
  1360. pend = virt_to_page(pg_vec[i] + (PAGE_SIZE << order) - 1);
  1361. for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
  1362. ClearPageReserved(page);
  1363. free_pages(pg_vec[i], order);
  1364. }
  1365. }
  1366. kfree(pg_vec);
  1367. }
  1368. static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
  1369. {
  1370. unsigned long *pg_vec = NULL;
  1371. struct tpacket_hdr **io_vec = NULL;
  1372. struct packet_opt *po = sk->protinfo.af_packet;
  1373. int order = 0;
  1374. int err = 0;
  1375. if (req->tp_block_nr) {
  1376. int i, l;
  1377. int frames_per_block;
  1378. /* Sanity tests and some calculations */
  1379. if ((int)req->tp_block_size <= 0)
  1380. return -EINVAL;
  1381. if (req->tp_block_size&(PAGE_SIZE-1))
  1382. return -EINVAL;
  1383. if (req->tp_frame_size < TPACKET_HDRLEN)
  1384. return -EINVAL;
  1385. if (req->tp_frame_size&(TPACKET_ALIGNMENT-1))
  1386. return -EINVAL;
  1387. frames_per_block = req->tp_block_size/req->tp_frame_size;
  1388. if (frames_per_block <= 0)
  1389. return -EINVAL;
  1390. if (frames_per_block*req->tp_block_nr != req->tp_frame_nr)
  1391. return -EINVAL;
  1392. /* OK! */
  1393. /* Allocate page vector */
  1394. while ((PAGE_SIZE<<order) < req->tp_block_size)
  1395. order++;
  1396. err = -ENOMEM;
  1397. pg_vec = kmalloc(req->tp_block_nr*sizeof(unsigned long*), GFP_KERNEL);
  1398. if (pg_vec == NULL)
  1399. goto out;
  1400. memset(pg_vec, 0, req->tp_block_nr*sizeof(unsigned long*));
  1401. for (i=0; i<req->tp_block_nr; i++) {
  1402. struct page *page, *pend;
  1403. pg_vec[i] = __get_free_pages(GFP_KERNEL, order);
  1404. if (!pg_vec[i])
  1405. goto out_free_pgvec;
  1406. memset((void *)(pg_vec[i]), 0, PAGE_SIZE << order);
  1407. pend = virt_to_page(pg_vec[i] + (PAGE_SIZE << order) - 1);
  1408. for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
  1409. SetPageReserved(page);
  1410. }
  1411. /* Page vector is allocated */
  1412. /* Draw frames */
  1413. io_vec = kmalloc(req->tp_frame_nr*sizeof(struct tpacket_hdr*), GFP_KERNEL);
  1414. if (io_vec == NULL)
  1415. goto out_free_pgvec;
  1416. memset(io_vec, 0, req->tp_frame_nr*sizeof(struct tpacket_hdr*));
  1417. l = 0;
  1418. for (i=0; i<req->tp_block_nr; i++) {
  1419. unsigned long ptr = pg_vec[i];
  1420. int k;
  1421. for (k=0; k<frames_per_block; k++, l++) {
  1422. io_vec[l] = (struct tpacket_hdr*)ptr;
  1423. io_vec[l]->tp_status = TP_STATUS_KERNEL;
  1424. ptr += req->tp_frame_size;
  1425. }
  1426. }
  1427. /* Done */
  1428. } else {
  1429. if (req->tp_frame_nr)
  1430. return -EINVAL;
  1431. }
  1432. lock_sock(sk);
  1433. /* Detach socket from network */
  1434. spin_lock(&po->bind_lock);
  1435. if (po->running)
  1436. dev_remove_pack(&po->prot_hook);
  1437. spin_unlock(&po->bind_lock);
  1438. err = -EBUSY;
  1439. if (closing || atomic_read(&po->mapped) == 0) {
  1440. err = 0;
  1441. #define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
  1442. spin_lock_bh(&sk->receive_queue.lock);
  1443. pg_vec = XC(po->pg_vec, pg_vec);
  1444. io_vec = XC(po->iovec, io_vec);
  1445. po->iovmax = req->tp_frame_nr-1;
  1446. po->head = 0;
  1447. po->frame_size = req->tp_frame_size;
  1448. spin_unlock_bh(&sk->receive_queue.lock);
  1449. order = XC(po->pg_vec_order, order);
  1450. req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
  1451. po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  1452. po->prot_hook.func = po->iovec ? tpacket_rcv : packet_rcv;
  1453. skb_queue_purge(&sk->receive_queue);
  1454. #undef XC
  1455. if (atomic_read(&po->mapped))
  1456. printk(KERN_DEBUG "packet_mmap: vma is busy: %dn", atomic_read(&po->mapped));
  1457. }
  1458. spin_lock(&po->bind_lock);
  1459. if (po->running)
  1460. dev_add_pack(&po->prot_hook);
  1461. spin_unlock(&po->bind_lock);
  1462. release_sock(sk);
  1463. if (io_vec)
  1464. kfree(io_vec);
  1465. out_free_pgvec:
  1466. if (pg_vec)
  1467. free_pg_vec(pg_vec, order, req->tp_block_nr);
  1468. out:
  1469. return err;
  1470. }
  1471. static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
  1472. {
  1473. struct sock *sk = sock->sk;
  1474. struct packet_opt *po = sk->protinfo.af_packet;
  1475. unsigned long size;
  1476. unsigned long start;
  1477. int err = -EINVAL;
  1478. int i;
  1479. if (vma->vm_pgoff)
  1480. return -EINVAL;
  1481. size = vma->vm_end - vma->vm_start;
  1482. lock_sock(sk);
  1483. if (po->pg_vec == NULL)
  1484. goto out;
  1485. if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
  1486. goto out;
  1487. atomic_inc(&po->mapped);
  1488. start = vma->vm_start;
  1489. err = -EAGAIN;
  1490. for (i=0; i<po->pg_vec_len; i++) {
  1491. if (remap_page_range(start, __pa(po->pg_vec[i]),
  1492.      po->pg_vec_pages*PAGE_SIZE,
  1493.      vma->vm_page_prot))
  1494. goto out;
  1495. start += po->pg_vec_pages*PAGE_SIZE;
  1496. }
  1497. vma->vm_ops = &packet_mmap_ops;
  1498. err = 0;
  1499. out:
  1500. release_sock(sk);
  1501. return err;
  1502. }
  1503. #endif
  1504. #ifdef CONFIG_SOCK_PACKET
  1505. struct proto_ops packet_ops_spkt = {
  1506. family: PF_PACKET,
  1507. release: packet_release,
  1508. bind: packet_bind_spkt,
  1509. connect: sock_no_connect,
  1510. socketpair: sock_no_socketpair,
  1511. accept: sock_no_accept,
  1512. getname: packet_getname_spkt,
  1513. poll: datagram_poll,
  1514. ioctl: packet_ioctl,
  1515. listen: sock_no_listen,
  1516. shutdown: sock_no_shutdown,
  1517. setsockopt: sock_no_setsockopt,
  1518. getsockopt: sock_no_getsockopt,
  1519. sendmsg: packet_sendmsg_spkt,
  1520. recvmsg: packet_recvmsg,
  1521. mmap: sock_no_mmap,
  1522. sendpage: sock_no_sendpage,
  1523. };
  1524. #endif
  1525. struct proto_ops packet_ops = {
  1526. family: PF_PACKET,
  1527. release: packet_release,
  1528. bind: packet_bind,
  1529. connect: sock_no_connect,
  1530. socketpair: sock_no_socketpair,
  1531. accept: sock_no_accept,
  1532. getname: packet_getname, 
  1533. poll: packet_poll,
  1534. ioctl: packet_ioctl,
  1535. listen: sock_no_listen,
  1536. shutdown: sock_no_shutdown,
  1537. setsockopt: packet_setsockopt,
  1538. getsockopt: packet_getsockopt,
  1539. sendmsg: packet_sendmsg,
  1540. recvmsg: packet_recvmsg,
  1541. mmap: packet_mmap,
  1542. sendpage: sock_no_sendpage,
  1543. };
  1544. static struct net_proto_family packet_family_ops = {
  1545. family: PF_PACKET,
  1546. create: packet_create,
  1547. };
  1548. static struct notifier_block packet_netdev_notifier = {
  1549. notifier_call: packet_notifier,
  1550. };
  1551. #ifdef CONFIG_PROC_FS
  1552. static int packet_read_proc(char *buffer, char **start, off_t offset,
  1553.      int length, int *eof, void *data)
  1554. {
  1555. off_t pos=0;
  1556. off_t begin=0;
  1557. int len=0;
  1558. struct sock *s;
  1559. len+= sprintf(buffer,"sk       RefCnt Type Proto  Iface R Rmem   User   Inoden");
  1560. read_lock(&packet_sklist_lock);
  1561. for (s = packet_sklist; s; s = s->next) {
  1562. len+=sprintf(buffer+len,"%p %-6d %-4d %04x   %-5d %1d %-6u %-6u %-6lu",
  1563.      s,
  1564.      atomic_read(&s->refcnt),
  1565.      s->type,
  1566.      ntohs(s->num),
  1567.      s->protinfo.af_packet->ifindex,
  1568.      s->protinfo.af_packet->running,
  1569.      atomic_read(&s->rmem_alloc),
  1570.      sock_i_uid(s),
  1571.      sock_i_ino(s)
  1572.      );
  1573. buffer[len++]='n';
  1574. pos=begin+len;
  1575. if(pos<offset) {
  1576. len=0;
  1577. begin=pos;
  1578. }
  1579. if(pos>offset+length)
  1580. goto done;
  1581. }
  1582. *eof = 1;
  1583. done:
  1584. read_unlock(&packet_sklist_lock);
  1585. *start=buffer+(offset-begin);
  1586. len-=(offset-begin);
  1587. if(len>length)
  1588. len=length;
  1589. if(len<0)
  1590. len=0;
  1591. return len;
  1592. }
  1593. #endif
  1594. static void __exit packet_exit(void)
  1595. {
  1596. remove_proc_entry("net/packet", 0);
  1597. unregister_netdevice_notifier(&packet_netdev_notifier);
  1598. sock_unregister(PF_PACKET);
  1599. return;
  1600. }
  1601. static int __init packet_init(void)
  1602. {
  1603. sock_register(&packet_family_ops);
  1604. register_netdevice_notifier(&packet_netdev_notifier);
  1605. #ifdef CONFIG_PROC_FS
  1606. create_proc_read_entry("net/packet", 0, 0, packet_read_proc, NULL);
  1607. #endif
  1608. return 0;
  1609. }
  1610. module_init(packet_init);
  1611. module_exit(packet_exit);
  1612. MODULE_LICENSE("GPL");