~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to modules/linux/vsock/linux/af_vsock.c

  • Committer: Package Import Robot
  • Author(s): Nate Muench
  • Date: 2012-06-20 15:59:51 UTC
  • mfrom: (1.4.8)
  • Revision ID: package-import@ubuntu.com-20120620155951-6rupmpb0f70b52zr
Tags: 2012.05.21-724730-0ubuntu1
* Merging upstream version 2012.05.21-724730.
  - Fixes building against the current Quantal kernel. (LP: #1000344)
  - Fixes Quantal installation issues. (LP: #1019031)

* Sync with Debian
  - Updating to debhelper version 9.
  - Updating to standards version 3.9.3.
  - Updating copyright file machine-readable format version 1.0.
  - Building without multiarch paths for now

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
#include <linux/list.h>
107
107
#include <linux/wait.h>
108
108
#include <linux/init.h>
 
109
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
 
110
#  include <net/tcp_states.h>
 
111
#else
 
112
#  include <linux/tcp.h>
 
113
#endif
109
114
#include <asm/io.h>
110
115
#if defined(__x86_64__) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 12)
111
116
#   include <linux/ioctl32.h>
169
174
                                         Bool oldPktProto);
170
175
static int VSockVmciGetAFValue(void);
171
176
static int VSockVmciRecvDgramCB(void *data, VMCIDatagram *dg);
 
177
static int VSockVmciRecvSeqCB(void *data, VMCIDatagram *dg);
172
178
static int VSockVmciRecvStreamCB(void *data, VMCIDatagram *dg);
173
179
static void VSockVmciPeerAttachCB(VMCIId subId,
174
180
                                  VMCI_EventData *ed, void *clientData);
210
216
                         struct sockaddr *addr, int addrLen);
211
217
static int VSockVmciDgramConnect(struct socket *sock,
212
218
                                 struct sockaddr *addr, int addrLen, int flags);
 
219
static int VSockVmciSeqConnect(struct socket *sock,
 
220
                               struct sockaddr *addr, int addrLen, int flags);
213
221
static int VSockVmciStreamConnect(struct socket *sock,
214
222
                                  struct sockaddr *addr, int addrLen, int flags);
215
223
static int VSockVmciAccept(struct socket *sock, struct socket *newsock, int flags);
236
244
                                 struct socket *sock, struct msghdr *msg, size_t len);
237
245
static int VSockVmciDgramRecvmsg(struct kiocb *kiocb, struct socket *sock,
238
246
                                 struct msghdr *msg, size_t len, int flags);
 
247
static int VSockVmciSeqSendmsg(struct kiocb *kiocb,
 
248
                               struct socket *sock, struct msghdr *msg, size_t len);
 
249
static int VSockVmciSeqRecvmsg(struct kiocb *kiocb, struct socket *sock,
 
250
                               struct msghdr *msg, size_t len, int flags);
239
251
static int VSockVmciStreamSendmsg(struct kiocb *kiocb,
240
252
                                 struct socket *sock, struct msghdr *msg, size_t len);
241
253
static int VSockVmciStreamRecvmsg(struct kiocb *kiocb, struct socket *sock,
297
309
   .owner  = THIS_MODULE,
298
310
};
299
311
 
300
 
/* Socket operations, split for DGRAM and STREAM sockets. */
 
312
/* Socket operations, split for DGRAM, STREAM and SEQPACKET sockets. */
301
313
static struct proto_ops vsockVmciDgramOps = {
302
314
   .family     = VSOCK_INVALID_FAMILY,
303
315
   .owner      = THIS_MODULE,
319
331
   .sendpage   = sock_no_sendpage,
320
332
};
321
333
 
 
334
static struct proto_ops vsockVmciSeqOps = {
 
335
   .family     = VSOCK_INVALID_FAMILY,
 
336
   .owner      = THIS_MODULE,
 
337
   .release    = VSockVmciRelease,
 
338
   .bind       = VSockVmciBind,
 
339
   .connect    = VSockVmciSeqConnect,
 
340
   .socketpair = sock_no_socketpair,
 
341
   .accept     = sock_no_accept,
 
342
   .getname    = VSockVmciGetname,
 
343
   .poll       = VSockVmciPoll,
 
344
   .ioctl      = sock_no_ioctl,
 
345
   .listen     = sock_no_listen,
 
346
   .shutdown   = VSockVmciShutdown,
 
347
   .setsockopt = sock_no_setsockopt,
 
348
   .getsockopt = sock_no_getsockopt,
 
349
   .sendmsg    = VSockVmciSeqSendmsg,
 
350
   .recvmsg    = VSockVmciSeqRecvmsg,
 
351
   .mmap       = sock_no_mmap,
 
352
   .sendpage   = sock_no_sendpage,
 
353
};
 
354
 
322
355
static struct proto_ops vsockVmciStreamOps = {
323
356
   .family     = VSOCK_INVALID_FAMILY,
324
357
   .owner      = THIS_MODULE,
373
406
static Bool vmciDevicePresent = FALSE;
374
407
static VMCIHandle vmciStreamHandle = { VMCI_INVALID_ID, VMCI_INVALID_ID };
375
408
static VMCIId qpResumedSubId = VMCI_INVALID_ID;
 
409
static VMCIId ctxUpdatedSubId = VMCI_INVALID_ID;
376
410
 
377
411
static int PROTOCOL_OVERRIDE = -1;
378
412
 
379
413
/*
380
 
 * Netperf benchmarks have shown significant throughput improvements when the QP
381
 
 * size is bumped from 64k to 256k. These measurements were taken during the K/L.next
382
 
 * timeframe. Give users better performance by default.
 
414
 * Netperf benchmarks have shown significant throughput improvements when the
 
415
 * QP size is bumped from 64k to 256k. These measurements were taken during the
 
416
 * K/L.next timeframe. Give users better performance by default.
383
417
 */
384
418
#define VSOCK_DEFAULT_QP_SIZE_MIN   128
385
419
#define VSOCK_DEFAULT_QP_SIZE       262144
391
425
 */
392
426
#define VSOCK_DEFAULT_CONNECT_TIMEOUT (2 * HZ)
393
427
 
 
428
#define VSOCK_SEND_SEQ_CLOSE(_vsk, _err) \
 
429
   VSockVmciSendSeqPacket((_vsk), VSOCK_SEQ_PACKET_TYPE_CLOSE, (_err))
 
430
#define VSOCK_SEND_SEQ_SHUTDOWN(_vsk, _mode) \
 
431
   VSockVmciSendSeqPacket((_vsk), VSOCK_SEQ_PACKET_TYPE_SHUTDOWN, (_mode))
 
432
 
394
433
#ifdef VMX86_DEVEL
395
434
# define LOG_PACKET(_pkt)  VSockVmciLogPkt(__FUNCTION__, __LINE__, _pkt)
396
435
#else
996
1035
      compat_sk_receive_skb(sk, skb, 0);
997
1036
   }
998
1037
 
999
 
   return 0;
 
1038
   return VMCI_SUCCESS;
 
1039
}
 
1040
 
 
1041
 
 
1042
/*
 
1043
 *----------------------------------------------------------------------------
 
1044
 *
 
1045
 * VSockVmciSendSeqPacket --
 
1046
 *
 
1047
 *    Send a sequential packet.  This uses a stack-allocated packet, i.e.,
 
1048
 *    it isn't meant for DATA packets, but it works fine for the other packet
 
1049
 *    types.
 
1050
 *
 
1051
 * Results:
 
1052
 *    Zero on success, negative error code on failure.
 
1053
 *
 
1054
 * Side effects:
 
1055
 *    None.
 
1056
 *
 
1057
 *----------------------------------------------------------------------------
 
1058
 */
 
1059
 
 
1060
static int
 
1061
VSockVmciSendSeqPacket(VSockVmciSock *vsk,      // IN
 
1062
                       VSockSeqPacketType type, // IN
 
1063
                       uint32 mode)             // IN
 
1064
{
 
1065
   int err;
 
1066
   VSockSeqPacket pkt;
 
1067
 
 
1068
   ASSERT(vsk);
 
1069
 
 
1070
   VSockSeqPacket_Init(&pkt, &vsk->localAddr, &vsk->remoteAddr, type, mode);
 
1071
 
 
1072
   err = VMCIDatagram_Send(&pkt.hdr.dg);
 
1073
   if (err < 0) {
 
1074
      err = VSockVmci_ErrorToVSockError(err);
 
1075
   }
 
1076
 
 
1077
   return err;
 
1078
}
 
1079
 
 
1080
 
 
1081
/*
 
1082
 *----------------------------------------------------------------------------
 
1083
 *
 
1084
 * VSockVmciRecvSeqCB --
 
1085
 *
 
1086
 *    VMCI Datagram receive callback.  This function is used specifically for
 
1087
 *    SOCK_SEQPACKET sockets.
 
1088
 *
 
1089
 *    This is invoked as part of a tasklet that's scheduled when the VMCI
 
1090
 *    interrupt fires.  This is run in bottom-half context and if it ever needs
 
1091
 *    to sleep it should defer that work to a work queue.
 
1092
 *
 
1093
 * Results:
 
1094
 *    Zero on success, negative error code on failure.
 
1095
 *
 
1096
 * Side effects:
 
1097
 *    An sk_buff is created and queued with this socket.
 
1098
 *
 
1099
 *----------------------------------------------------------------------------
 
1100
 */
 
1101
 
 
1102
static int
 
1103
VSockVmciRecvSeqCB(void *data,          // IN
 
1104
                   VMCIDatagram *dg)    // IN
 
1105
{
 
1106
   struct sock *sk;
 
1107
   size_t size;
 
1108
   VSockVmciSock *vsk;
 
1109
   VSockSeqPacket *pkt;
 
1110
 
 
1111
   ASSERT(dg);
 
1112
   ASSERT(dg->payloadSize <= VMCI_MAX_DG_PAYLOAD_SIZE);
 
1113
 
 
1114
   sk = (struct sock *)data;
 
1115
 
 
1116
   ASSERT(sk);
 
1117
 
 
1118
   /* XXX, figure out why sk->sk_socket can be NULL. */
 
1119
   if (!sk->sk_socket) {
 
1120
      return EINVAL;
 
1121
   }
 
1122
 
 
1123
   ASSERT(sk->sk_socket->type == SOCK_SEQPACKET);
 
1124
 
 
1125
   if (VMCI_HYPERVISOR_CONTEXT_ID != dg->src.context) {
 
1126
      return VMCI_ERROR_NO_ACCESS;
 
1127
   }
 
1128
 
 
1129
   if (VMCI_RPC_PRIVILEGED != dg->src.resource &&
 
1130
       VMCI_RPC_UNPRIVILEGED != dg->src.resource) {
 
1131
      return VMCI_ERROR_NO_ACCESS;
 
1132
   }
 
1133
 
 
1134
   size = VMCI_DG_SIZE(dg);
 
1135
   if (size < sizeof *pkt) {
 
1136
      return VMCI_ERROR_INVALID_ARGS;
 
1137
   }
 
1138
 
 
1139
   vsk = vsock_sk(sk);
 
1140
   pkt = (VSockSeqPacket *)dg;
 
1141
 
 
1142
   /*
 
1143
    * After this point, if we fail to handle the packet, we need to send a
 
1144
    * close to the peer with an error.  Otherwise it might hang, waiting for a
 
1145
    * response to a packet that we discarded.
 
1146
    */
 
1147
 
 
1148
   if (VSOCK_SEQ_PACKET_VERSION_1 != pkt->hdr.version) {
 
1149
      VSOCK_SEND_SEQ_CLOSE(vsk, EINVAL);
 
1150
      return VMCI_ERROR_INVALID_ARGS;
 
1151
   }
 
1152
 
 
1153
   if (SS_CONNECTED != sk->sk_socket->state) {
 
1154
      VSOCK_SEND_SEQ_CLOSE(vsk, ENOTCONN);
 
1155
      return VMCI_ERROR_DST_UNREACHABLE;
 
1156
   }
 
1157
 
 
1158
   switch (pkt->hdr.type) {
 
1159
   case VSOCK_SEQ_PACKET_TYPE_DATA: {
 
1160
      struct sk_buff *skb;
 
1161
      /*
 
1162
       * Attach the packet to the socket's receive queue as an sk_buff.
 
1163
       */
 
1164
 
 
1165
      size -= sizeof *pkt;
 
1166
      skb = alloc_skb(size, GFP_ATOMIC);
 
1167
      if (!skb) {
 
1168
         VSOCK_SEND_SEQ_CLOSE(vsk, ENOMEM);
 
1169
         return VMCI_ERROR_NO_MEM;
 
1170
      }
 
1171
 
 
1172
      /* compat_sk_receive_skb() will do a sock_put(), so hold here. */
 
1173
      sock_hold(sk);
 
1174
      skb_put(skb, size);
 
1175
      memcpy(skb->data, VSOCK_SEQ_PACKET_PAYLOAD(pkt), size);
 
1176
 
 
1177
      /*
 
1178
       * XXX, this can drop the skb.  We need to find an alternative that
 
1179
       * will return an error if that happens, so that we can send a reset
 
1180
       * to the peer, i.e.,
 
1181
       *
 
1182
       * if (!receive_skb(sk, skb)) {
 
1183
       *    VSOCK_SEND_SEQ_CLOSE(vsk, ENOMEM);
 
1184
       *    return VMCI_ERROR_NO_MEM;
 
1185
       * }
 
1186
       */
 
1187
 
 
1188
      compat_sk_receive_skb(sk, skb, 0);
 
1189
      break;
 
1190
   }
 
1191
   case VSOCK_SEQ_PACKET_TYPE_CLOSE:
 
1192
      bh_lock_sock(sk);
 
1193
 
 
1194
      sock_set_flag(sk, SOCK_DONE);
 
1195
      vsk->peerShutdown = SHUTDOWN_MASK;
 
1196
      sk->sk_state = TCP_CLOSE;
 
1197
 
 
1198
      /*
 
1199
       * A close packet with an error code means a forceful reset, whereas
 
1200
       * no error means a graceful close.
 
1201
       */
 
1202
      if (pkt->hdr.val) {
 
1203
         sk->sk_socket->state = SS_UNCONNECTED;
 
1204
         sk->sk_err = pkt->hdr.val;
 
1205
         sk->sk_error_report(sk);
 
1206
      } else {
 
1207
         if (skb_queue_empty(&sk->sk_receive_queue)) {
 
1208
            sk->sk_socket->state = SS_DISCONNECTING;
 
1209
         }
 
1210
         sk->sk_state_change(sk);
 
1211
      }
 
1212
 
 
1213
      bh_unlock_sock(sk);
 
1214
      break;
 
1215
   /*
 
1216
    * There's no reason for us to receive a shutdown packet in this direction,
 
1217
    * or any other packet for that matter.  Inform the peer that the packet
 
1218
    * is invalid.
 
1219
    */
 
1220
   default:
 
1221
      VSOCK_SEND_SEQ_CLOSE(vsk, EINVAL);
 
1222
      return VMCI_ERROR_INVALID_ARGS;
 
1223
   }
 
1224
 
 
1225
   return VMCI_SUCCESS;
1000
1226
}
1001
1227
 
1002
1228
 
1386
1612
/*
1387
1613
 *----------------------------------------------------------------------------
1388
1614
 *
 
1615
 * VSockVmciContextUpdatedCB --
 
1616
 *
 
1617
 *    Invoked when a VM is resumed (technically when the context ID changes,
 
1618
 *    but the event is actually sent even when it does not, so this works
 
1619
 *    well for catching resumes).  We must mark all connected sequential
 
1620
 *    sockets as detached.
 
1621
 *
 
1622
 * Results:
 
1623
 *    None.
 
1624
 *
 
1625
 * Side effects:
 
1626
 *    May modify socket state and signal socket.
 
1627
 *
 
1628
 *----------------------------------------------------------------------------
 
1629
 */
 
1630
 
 
1631
static void
 
1632
VSockVmciContextUpdatedCB(VMCIId subId,          // IN
 
1633
                          VMCI_EventData *eData, // IN
 
1634
                          void *clientData)      // IN
 
1635
{
 
1636
   uint32 i;
 
1637
 
 
1638
   spin_lock_bh(&vsockSeqTableLock);
 
1639
 
 
1640
   for (i = 0; i < ARRAYSIZE(vsockSeqTable); i++) {
 
1641
      VSockVmciSock *vsk;
 
1642
 
 
1643
      list_for_each_entry(vsk, &vsockSeqTable[i], seqTable) {
 
1644
         struct sock *sk = sk_vsock(vsk);
 
1645
 
 
1646
         sock_set_flag(sk, SOCK_DONE);
 
1647
         vsk->peerShutdown = SHUTDOWN_MASK;
 
1648
         sk->sk_state = TCP_CLOSE;
 
1649
 
 
1650
         if (skb_queue_empty(&sk->sk_receive_queue)) {
 
1651
            sk->sk_socket->state = SS_DISCONNECTING;
 
1652
         }
 
1653
         sk->sk_state_change(sk);
 
1654
      }
 
1655
   }
 
1656
 
 
1657
   spin_unlock_bh(&vsockSeqTableLock);
 
1658
}
 
1659
 
 
1660
 
 
1661
/*
 
1662
 *----------------------------------------------------------------------------
 
1663
 *
1389
1664
 * VSockVmciPendingWork --
1390
1665
 *
1391
1666
 *    Releases the resources for a pending socket if it has not reached the
2680
2955
      }
2681
2956
      break;
2682
2957
   }
2683
 
   case SOCK_DGRAM: {
 
2958
   case SOCK_DGRAM:
 
2959
   case SOCK_SEQPACKET: {
2684
2960
      uint32 flags = 0;
2685
2961
 
2686
2962
      /* VMCI will select a resource ID for us if we provide VMCI_INVALID_ID. */
2699
2975
      }
2700
2976
 
2701
2977
      err = VSockVmciDatagramCreateHnd(newAddr.svm_port, flags,
2702
 
                                       VSockVmciRecvDgramCB, sk,
2703
 
                                       &vsk->dgHandle);
 
2978
                                       sk->sk_socket->type == SOCK_DGRAM ?
 
2979
                                          VSockVmciRecvDgramCB :
 
2980
                                          VSockVmciRecvSeqCB,
 
2981
                                       sk, &vsk->dgHandle);
2704
2982
      if (err < VMCI_SUCCESS) {
2705
2983
         err = VSockVmci_ErrorToVSockError(err);
2706
2984
         goto out;
2838
3116
 
2839
3117
   sk->sk_destruct = VSockVmciSkDestruct;
2840
3118
   sk->sk_backlog_rcv = VSockVmciQueueRcvSkb;
2841
 
   sk->sk_state = SS_UNCONNECTED;
 
3119
   sk->sk_state = 0;
2842
3120
   sock_reset_flag(sk, SOCK_DONE);
2843
3121
 
2844
3122
   INIT_LIST_HEAD(&vsk->boundTable);
2845
3123
   INIT_LIST_HEAD(&vsk->connectedTable);
 
3124
   INIT_LIST_HEAD(&vsk->seqTable);
2846
3125
   vsk->dgHandle = VMCI_INVALID_HANDLE;
2847
3126
   vsk->qpHandle = VMCI_INVALID_HANDLE;
2848
3127
   vsk->qpair = NULL;
2918
3197
         VSockVmciRemoveConnected(sk);
2919
3198
      }
2920
3199
 
 
3200
      if (VSockVmciInSeqTable(sk)) {
 
3201
         VSockVmciRemoveSeq(sk);
 
3202
      }
 
3203
 
2921
3204
      if (!VMCI_HANDLE_INVALID(vsk->dgHandle)) {
 
3205
         if (SOCK_SEQPACKET == sk->sk_type && TCP_ESTABLISHED == sk->sk_state) {
 
3206
            VSOCK_SEND_SEQ_CLOSE(vsk, 0);
 
3207
         }
2922
3208
         VMCIDatagram_DestroyHnd(vsk->dgHandle);
2923
3209
         vsk->dgHandle = VMCI_INVALID_HANDLE;
2924
3210
      }
3013
3299
   VSockVmciTestUnregister();
3014
3300
   compat_mutex_unlock(&registrationMutex);
3015
3301
 
3016
 
 
3017
3302
   VSOCK_STATS_CTLPKT_DUMP_ALL();
3018
3303
   VSOCK_STATS_HIST_DUMP_ALL();
 
3304
   VSOCK_STATS_TOTALS_DUMP_ALL();
3019
3305
}
3020
3306
 
3021
3307
 
3168
3454
      } else {
3169
3455
         vsockVmciDgramOps.family = i;
3170
3456
         vsockVmciStreamOps.family = i;
 
3457
         vsockVmciSeqOps.family = i;
3171
3458
         err = i;
3172
3459
         break;
3173
3460
      }
3204
3491
 
3205
3492
   vsockVmciDgramOps.family = vsockVmciFamilyOps.family = VSOCK_INVALID_FAMILY;
3206
3493
   vsockVmciStreamOps.family = vsockVmciFamilyOps.family;
 
3494
   vsockVmciSeqOps.family = vsockVmciFamilyOps.family;
3207
3495
}
3208
3496
 
3209
3497
 
3269
3557
      goto out;
3270
3558
   }
3271
3559
 
 
3560
   err = VMCIEvent_Subscribe(VMCI_EVENT_CTX_ID_UPDATE,
 
3561
                             VMCI_FLAG_EVENT_NONE,
 
3562
                             VSockVmciContextUpdatedCB,
 
3563
                             NULL,
 
3564
                             &ctxUpdatedSubId);
 
3565
   if (err < VMCI_SUCCESS) {
 
3566
      Warning("Unable to subscribe to context updated event. (%d)\n", err);
 
3567
      err = VSockVmci_ErrorToVSockError(err);
 
3568
      ctxUpdatedSubId = VMCI_INVALID_ID;
 
3569
      goto out;
 
3570
   }
 
3571
 
3272
3572
out:
3273
3573
   if (err != 0) {
3274
3574
      VSockVmciUnregisterWithVmci();
3315
3615
      qpResumedSubId = VMCI_INVALID_ID;
3316
3616
   }
3317
3617
 
 
3618
   if (ctxUpdatedSubId != VMCI_INVALID_ID) {
 
3619
      VMCIEvent_Unsubscribe(ctxUpdatedSubId);
 
3620
      ctxUpdatedSubId = VMCI_INVALID_ID;
 
3621
   }
 
3622
 
3318
3623
   VMCI_DeviceRelease(NULL);
3319
3624
   vmciDevicePresent = FALSE;
3320
3625
}
3492
3797
 
3493
3798
   lock_sock(sk);
3494
3799
 
3495
 
 
3496
 
   if (!VSockAddr_Bound(&vsk->localAddr)) {
3497
 
      struct sockaddr_vm localAddr;
3498
 
 
3499
 
      VSockAddr_Init(&localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
3500
 
      if ((err = __VSockVmciBind(sk, &localAddr))) {
3501
 
         goto out;
3502
 
      }
3503
 
   }
3504
 
 
3505
 
   if (!VSockAddr_SocketContextDgram(remoteAddr->svm_cid,
3506
 
                                     remoteAddr->svm_port)) {
3507
 
      err = -EINVAL;
3508
 
      goto out;
3509
 
   }
3510
 
 
3511
 
   memcpy(&vsk->remoteAddr, remoteAddr, sizeof vsk->remoteAddr);
3512
 
   sock->state = SS_CONNECTED;
 
3800
   if (!VSockAddr_Bound(&vsk->localAddr)) {
 
3801
      struct sockaddr_vm localAddr;
 
3802
 
 
3803
      VSockAddr_Init(&localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
3804
      if ((err = __VSockVmciBind(sk, &localAddr))) {
 
3805
         goto out;
 
3806
      }
 
3807
   }
 
3808
 
 
3809
   if (!VSockAddr_SocketContextDgram(remoteAddr->svm_cid,
 
3810
                                     remoteAddr->svm_port)) {
 
3811
      err = -EINVAL;
 
3812
      goto out;
 
3813
   }
 
3814
 
 
3815
   memcpy(&vsk->remoteAddr, remoteAddr, sizeof vsk->remoteAddr);
 
3816
   sock->state = SS_CONNECTED;
 
3817
 
 
3818
out:
 
3819
   release_sock(sk);
 
3820
   return err;
 
3821
}
 
3822
 
 
3823
 
 
3824
/*
 
3825
 *----------------------------------------------------------------------------
 
3826
 *
 
3827
 * VSockVmciSeqConnect --
 
3828
 *
 
3829
 *    Connects a sequential socket.
 
3830
 *
 
3831
 * Results:
 
3832
 *    Zero on success, negative error code on failure.
 
3833
 *
 
3834
 * Side effects:
 
3835
 *    None.
 
3836
 *
 
3837
 *----------------------------------------------------------------------------
 
3838
 */
 
3839
 
 
3840
static int
 
3841
VSockVmciSeqConnect(struct socket *sock,   // IN
 
3842
                    struct sockaddr *addr, // IN
 
3843
                    int addrLen,           // IN
 
3844
                    int flags)             // IN
 
3845
{
 
3846
   int err;
 
3847
   struct sock *sk;
 
3848
   VSockVmciSock *vsk;
 
3849
   VSockSeqPacket pkt;
 
3850
   struct sockaddr_vm *remoteAddr;
 
3851
 
 
3852
   sk = sock->sk;
 
3853
   vsk = vsock_sk(sk);
 
3854
 
 
3855
   lock_sock(sk);
 
3856
 
 
3857
   if (SS_CONNECTED == sock->state) {
 
3858
      err = -EISCONN;
 
3859
      goto out;
 
3860
   } else if (SS_CONNECTING == sock->state ||
 
3861
              SS_DISCONNECTING == sock->state) {
 
3862
      err = -EINVAL;
 
3863
      goto out;
 
3864
   }
 
3865
 
 
3866
   if (VSockAddr_Cast(addr, addrLen, &remoteAddr) != 0) {
 
3867
      err = -EINVAL;
 
3868
      goto out;
 
3869
   }
 
3870
 
 
3871
   if (!VSockAddr_Bound(&vsk->localAddr)) {
 
3872
      struct sockaddr_vm localAddr;
 
3873
 
 
3874
      VSockAddr_Init(&localAddr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
 
3875
      if ((err = __VSockVmciBind(sk, &localAddr))) {
 
3876
         goto out;
 
3877
      }
 
3878
   }
 
3879
 
 
3880
   if (VMCI_HYPERVISOR_CONTEXT_ID != remoteAddr->svm_cid) {
 
3881
      err = -EINVAL;
 
3882
      goto out;
 
3883
   }
 
3884
 
 
3885
   if (VMCI_RPC_PRIVILEGED != remoteAddr->svm_port &&
 
3886
       VMCI_RPC_UNPRIVILEGED != remoteAddr->svm_port) {
 
3887
      err = -EINVAL;
 
3888
      goto out;
 
3889
   }
 
3890
 
 
3891
   if (!VSockAddr_SocketContextDgram(remoteAddr->svm_cid,
 
3892
                                     remoteAddr->svm_port)) {
 
3893
      err = -EINVAL;
 
3894
      goto out;
 
3895
   }
 
3896
 
 
3897
   VSockSeqPacket_Init(&pkt, &vsk->localAddr, remoteAddr,
 
3898
                       VSOCK_SEQ_PACKET_TYPE_CONNECT, 0);
 
3899
 
 
3900
   err = VMCIDatagram_Send(&pkt.hdr.dg);
 
3901
   if (err < 0) {
 
3902
      err = VSockVmci_ErrorToVSockError(err);
 
3903
      goto out;
 
3904
   }
 
3905
 
 
3906
   /*
 
3907
    * It's not necessary to get an acknowledgement.  We're sending to the
 
3908
    * hypervisor, which means the result of the call tells us whether the
 
3909
    * endpoint accepted it or not.  So as long as it returns success,
 
3910
    * we are connected.
 
3911
    */
 
3912
 
 
3913
   memcpy(&vsk->remoteAddr, remoteAddr, sizeof vsk->remoteAddr);
 
3914
 
 
3915
   /*
 
3916
    * The skb routines actually check if this is a sequential socket, and if
 
3917
    * so, they require that the socket be in the TCP established state.  So
 
3918
    * we need to use the TCP states for sk_state rather than the SS states
 
3919
    * (our STREAM sockets cheat and get away with it, we should fix that).
 
3920
    */
 
3921
 
 
3922
   sock->state = SS_CONNECTED;
 
3923
   sk->sk_state = TCP_ESTABLISHED;
 
3924
   VSockVmciInsertSeq(vsockSeqSocketsVsk(vsk), sk);
 
3925
   sk->sk_state_change(sk);
 
3926
 
 
3927
   err = 0;
3513
3928
 
3514
3929
out:
3515
3930
   release_sock(sk);
3966
4381
         mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
3967
4382
      }
3968
4383
   } else if (sock->type == SOCK_STREAM) {
3969
 
      VSockVmciSock *vsk;
3970
 
 
3971
4384
      lock_sock(sk);
3972
4385
 
3973
 
      vsk = vsock_sk(sk);
3974
 
 
3975
4386
      /*
3976
4387
       * Listening sockets that have connections in their accept queue can be read.
3977
4388
       */
3997
4408
      }
3998
4409
 
3999
4410
      /*
4000
 
       * Sockets whose connections have been close, reset, or terminated should also
4001
 
       * be considered read, and we check the shutdown flag for that.
 
4411
       * Sockets whose connections have been closed, reset, or terminated
 
4412
       * should also be considered read, and we check the shutdown flag for
 
4413
       * that.
4002
4414
       */
4003
4415
      if (sk->sk_shutdown & RCV_SHUTDOWN ||
4004
4416
          vsk->peerShutdown & SEND_SHUTDOWN) {
4036
4448
      }
4037
4449
 
4038
4450
      release_sock(sk);
 
4451
   } else if (sock->type == SOCK_SEQPACKET) {
 
4452
      lock_sock(sk);
 
4453
 
 
4454
      /*
 
4455
       * If there is something in the queue then we can read.
 
4456
       */
 
4457
      if (!skb_queue_empty(&sk->sk_receive_queue) &&
 
4458
          !(sk->sk_shutdown & RCV_SHUTDOWN)) {
 
4459
         mask |= POLLIN | POLLRDNORM;
 
4460
      }
 
4461
 
 
4462
      /*
 
4463
       * Sockets whose connections have beed closed, reset or terminated
 
4464
       * should also be considered readable, and we check the shutdown flag
 
4465
       * for that.
 
4466
       */
 
4467
      if (sk->sk_shutdown & RCV_SHUTDOWN ||
 
4468
          vsk->peerShutdown & SEND_SHUTDOWN) {
 
4469
         mask |= POLLIN | POLLRDNORM;
 
4470
      }
 
4471
 
 
4472
      /*
 
4473
       * Connected sockets that can produce data can be written.
 
4474
       */
 
4475
      if (sk->sk_state == TCP_ESTABLISHED &&
 
4476
          !(sk->sk_shutdown & SEND_SHUTDOWN)) {
 
4477
         mask |= POLLOUT | POLLWRNORM;
 
4478
      }
 
4479
 
 
4480
      release_sock(sk);
4039
4481
   }
4040
4482
 
4041
4483
   return mask;
4135
4577
   }
4136
4578
 
4137
4579
   /*
4138
 
    * If this is a STREAM socket and it is not connected then bail out
4139
 
    * immediately.  If it is a DGRAM socket then we must first kick the socket
4140
 
    * so that it wakes up from any sleeping calls, for example recv(), and then
4141
 
    * afterwards return the error.
 
4580
    * If this is a STREAM/SEQPACKET socket and it is not connected then bail
 
4581
    * out immediately.  If it is a DGRAM socket then we must first kick the
 
4582
    * socket so that it wakes up from any sleeping calls, for example recv(),
 
4583
    * and then afterwards return the error.
4142
4584
    */
4143
4585
 
4144
4586
   sk = sock->sk;
4145
4587
   if (sock->state == SS_UNCONNECTED) {
4146
4588
      err = -ENOTCONN;
4147
 
      if (sk->sk_type == SOCK_STREAM) {
 
4589
      if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
4148
4590
         return err;
4149
4591
      }
4150
4592
   } else {
4152
4594
      err = 0;
4153
4595
   }
4154
4596
 
 
4597
   /*
 
4598
    * It doesn't make any sense to try and shutdown a sequential socket to
 
4599
    * the hypervisor in the recv direction, only for send or for both.
 
4600
    */
 
4601
 
 
4602
   if (sk->sk_type == SOCK_SEQPACKET && mode == RCV_SHUTDOWN) {
 
4603
      err = -EINVAL;
 
4604
      return err;
 
4605
   }
 
4606
 
4155
4607
   /* Receive and send shutdowns are treated alike. */
4156
4608
   mode = mode & (RCV_SHUTDOWN | SEND_SHUTDOWN);
4157
4609
   if (mode) {
4158
4610
      lock_sock(sk);
4159
4611
      sk->sk_shutdown |= mode;
 
4612
      if (sk->sk_type == SOCK_SEQPACKET) {
 
4613
         sk->sk_state = TCP_CLOSE;
 
4614
      }
4160
4615
      sk->sk_state_change(sk);
4161
4616
      release_sock(sk);
4162
 
   }
4163
4617
 
4164
 
   if (sk->sk_type == SOCK_STREAM && mode) {
4165
 
      sock_reset_flag(sk, SOCK_DONE);
4166
 
      VSOCK_SEND_SHUTDOWN(sk, mode);
 
4618
      if (sk->sk_type == SOCK_STREAM) {
 
4619
         sock_reset_flag(sk, SOCK_DONE);
 
4620
         VSOCK_SEND_SHUTDOWN(sk, mode);
 
4621
      } else if (sk->sk_type == SOCK_SEQPACKET) {
 
4622
         sock_reset_flag(sk, SOCK_DONE);
 
4623
         err = VSOCK_SEND_SEQ_SHUTDOWN(vsock_sk(sk), mode);
 
4624
      }
4167
4625
   }
4168
4626
 
4169
4627
   return err;
4293
4751
      goto out;
4294
4752
   }
4295
4753
 
4296
 
   /*
4297
 
    * err is the number of bytes sent on success.  We need to subtract the
4298
 
    * VSock-specific header portions of what we've sent.
4299
 
    */
4300
4754
   err -= sizeof *dg;
4301
4755
 
4302
4756
out:
4304
4758
   return err;
4305
4759
}
4306
4760
 
 
4761
 
 
4762
/*
 
4763
 *----------------------------------------------------------------------------
 
4764
 *
 
4765
 * VSockVmciSeqSendmsg --
 
4766
 *
 
4767
 *    Sends a datagram.
 
4768
 *
 
4769
 * Results:
 
4770
 *    Number of bytes sent on success, negative error code on failure.
 
4771
 *
 
4772
 * Side effects:
 
4773
 *    None.
 
4774
 *
 
4775
 *----------------------------------------------------------------------------
 
4776
 */
 
4777
 
 
4778
static int
 
4779
VSockVmciSeqSendmsg(struct kiocb *kiocb,          // UNUSED
 
4780
                    struct socket *sock,          // IN: socket to send on
 
4781
                    struct msghdr *msg,           // IN: message to send
 
4782
                    size_t len)                   // IN: length of message
 
4783
{
 
4784
   int err;
 
4785
   struct sock *sk;
 
4786
   VSockVmciSock *vsk;
 
4787
   VSockSeqPacket *pkt;
 
4788
 
 
4789
   sk = sock->sk;
 
4790
   vsk = vsock_sk(sk);
 
4791
 
 
4792
   if (msg->msg_flags & MSG_OOB) {
 
4793
      return -EOPNOTSUPP;
 
4794
   }
 
4795
 
 
4796
   if (len > VMCI_MAX_DG_PAYLOAD_SIZE) {
 
4797
      return -EMSGSIZE;
 
4798
   }
 
4799
 
 
4800
   lock_sock(sk);
 
4801
 
 
4802
   /* Callers should not provide a destination with sequential sockets. */
 
4803
   if (msg->msg_namelen) {
 
4804
      err = sock->state == SS_CONNECTED ? -EISCONN : -EOPNOTSUPP;
 
4805
      goto out;
 
4806
   }
 
4807
 
 
4808
   /* Send data only if we're not shutdown in that direction. */
 
4809
   if (sk->sk_shutdown & SEND_SHUTDOWN) {
 
4810
      err = -EPIPE;
 
4811
      goto out;
 
4812
   }
 
4813
 
 
4814
   if (sock->state != SS_CONNECTED) {
 
4815
      err = -ENOTCONN;
 
4816
      goto out;
 
4817
   }
 
4818
 
 
4819
   /*
 
4820
    * We already managed to connect, which means we must already have the
 
4821
    * right privs to send to our peer.  So no need for the usual datagram
 
4822
    * checks here, they were done by connect().
 
4823
    */
 
4824
 
 
4825
   /*
 
4826
    * Allocate a buffer for the user's message and our packet header.
 
4827
    */
 
4828
   pkt = kmalloc(len + sizeof *pkt, GFP_KERNEL);
 
4829
   if (!pkt) {
 
4830
      err = -ENOMEM;
 
4831
      goto out;
 
4832
   }
 
4833
 
 
4834
   VSockSeqPacket_Init(pkt, &vsk->localAddr, &vsk->remoteAddr,
 
4835
                       VSOCK_SEQ_PACKET_TYPE_DATA, 0);
 
4836
   pkt->hdr.dg.payloadSize += len;
 
4837
 
 
4838
   err = memcpy_fromiovec(VSOCK_SEQ_PACKET_PAYLOAD(pkt), msg->msg_iov, len);
 
4839
   if (0 != err) {
 
4840
      kfree(pkt);
 
4841
      goto out;
 
4842
   }
 
4843
 
 
4844
   err = VMCIDatagram_Send(&pkt->hdr.dg);
 
4845
   kfree(pkt);
 
4846
   if (err < 0) {
 
4847
      err = VSockVmci_ErrorToVSockError(err);
 
4848
      goto out;
 
4849
   }
 
4850
 
 
4851
   err = len;
 
4852
 
 
4853
out:
 
4854
   release_sock(sk);
 
4855
   return err;
 
4856
}
 
4857
 
 
4858
 
4307
4859
/*
4308
4860
 *----------------------------------------------------------------------------
4309
4861
 *
4661
5213
 
4662
5214
outWait:
4663
5215
   if (totalWritten > 0) {
 
5216
      VSOCK_STATS_STREAM_PRODUCE(totalWritten);
4664
5217
      err = totalWritten;
4665
5218
   }
4666
5219
   finish_wait(sk_sleep(sk), &wait);
4670
5223
}
4671
5224
 
4672
5225
 
 
5226
 
4673
5227
/*
4674
5228
 *----------------------------------------------------------------------------
4675
5229
 *
4699
5253
   VMCIDatagram *dg;
4700
5254
   size_t payloadLen;
4701
5255
   struct sk_buff *skb;
4702
 
   struct sockaddr_vm *vmciAddr;
4703
5256
 
4704
 
   err = 0;
4705
5257
   sk = sock->sk;
4706
 
   payloadLen = 0;
4707
5258
   noblock = flags & MSG_DONTWAIT;
4708
 
   vmciAddr = (struct sockaddr_vm *)msg->msg_name;
4709
5259
 
4710
5260
   if (flags & MSG_OOB || flags & MSG_ERRQUEUE) {
4711
5261
      return -EOPNOTSUPP;
4712
5262
   }
4713
5263
 
4714
5264
   /* Retrieve the head sk_buff from the socket's receive queue. */
 
5265
   err = 0;
4715
5266
   skb = skb_recv_datagram(sk, flags, noblock, &err);
4716
5267
   if (err) {
4717
5268
      return err;
4746
5297
   }
4747
5298
 
4748
5299
   msg->msg_namelen = 0;
4749
 
   if (vmciAddr) {
 
5300
   if (msg->msg_name) {
 
5301
      struct sockaddr_vm *vmciAddr;
 
5302
 
4750
5303
      /* Provide the address of the sender. */
 
5304
      vmciAddr = (struct sockaddr_vm *)msg->msg_name;
4751
5305
      VSockAddr_Init(vmciAddr,
4752
5306
                     VMCI_HANDLE_TO_CONTEXT_ID(dg->src),
4753
5307
                     VMCI_HANDLE_TO_RESOURCE_ID(dg->src));
4764
5318
/*
4765
5319
 *----------------------------------------------------------------------------
4766
5320
 *
 
5321
 * VSockVmciSeqRecvmsg --
 
5322
 *
 
5323
 *    Receives a datagram and places it in the caller's msg.
 
5324
 *
 
5325
 * Results:
 
5326
 *    The size of the payload on success, negative value on failure.
 
5327
 *
 
5328
 * Side effects:
 
5329
 *    None.
 
5330
 *
 
5331
 *----------------------------------------------------------------------------
 
5332
 */
 
5333
 
 
5334
static int
 
5335
VSockVmciSeqRecvmsg(struct kiocb *kiocb,          // UNUSED
 
5336
                    struct socket *sock,          // IN: socket to receive from
 
5337
                    struct msghdr *msg,           // IN/OUT: message to receive into
 
5338
                    size_t len,                   // IN: length of receive buffer
 
5339
                    int flags)                    // IN: receive flags
 
5340
{
 
5341
   int err;
 
5342
   int noblock;
 
5343
   size_t payloadLen;
 
5344
   struct sock *sk;
 
5345
   struct sk_buff *skb;
 
5346
 
 
5347
   if (flags & MSG_OOB || flags & MSG_ERRQUEUE) {
 
5348
      return -EOPNOTSUPP;
 
5349
   }
 
5350
 
 
5351
   sk = sock->sk;
 
5352
   noblock = flags & MSG_DONTWAIT;
 
5353
 
 
5354
   /* Retrieve the head sk_buff from the socket's receive queue. */
 
5355
   err = 0;
 
5356
   skb = skb_recv_datagram(sk, flags, noblock, &err);
 
5357
   if (err) {
 
5358
      return err;
 
5359
   }
 
5360
 
 
5361
   if (!skb) {
 
5362
      return -EAGAIN;
 
5363
   }
 
5364
 
 
5365
   if (!skb->data) {
 
5366
      /* err is 0, meaning we read zero bytes. */
 
5367
      goto out;
 
5368
   }
 
5369
 
 
5370
   payloadLen = skb->len;
 
5371
   if (payloadLen > len) {
 
5372
      payloadLen = len;
 
5373
      msg->msg_flags |= MSG_TRUNC;
 
5374
      /*
 
5375
       * XXX, we're supposed to be a reliable protocol, so while it's fine to
 
5376
       * return a partial packet here, we shouldn't drop the remainder.  We
 
5377
       * should keep it around so that a subsequent recv() can read it and
 
5378
       * then get the end of record marker (see below).
 
5379
       */
 
5380
   } else {
 
5381
      /* We managed to read the whole payload, so mark the end of record. */
 
5382
      msg->msg_flags |= MSG_EOR;
 
5383
   }
 
5384
 
 
5385
   /* Place the datagram payload in the user's iovec. */
 
5386
   err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, payloadLen);
 
5387
   if (err) {
 
5388
      goto out;
 
5389
   }
 
5390
 
 
5391
   msg->msg_namelen = 0;
 
5392
   if (msg->msg_name) {
 
5393
      VSockVmciSock *vsk;
 
5394
      struct sockaddr_vm *vmciAddr;
 
5395
 
 
5396
      /* Provide the address of the sender. */
 
5397
      vsk = vsock_sk(sk);
 
5398
      vmciAddr = (struct sockaddr_vm *)msg->msg_name;
 
5399
      VSockAddr_Init(vmciAddr,
 
5400
                     vsk->remoteAddr.svm_cid, vsk->remoteAddr.svm_port);
 
5401
      msg->msg_namelen = sizeof *vmciAddr;
 
5402
   }
 
5403
   err = payloadLen;
 
5404
 
 
5405
out:
 
5406
   skb_free_datagram(sk, skb);
 
5407
   return err;
 
5408
}
 
5409
 
 
5410
 
 
5411
/*
 
5412
 *----------------------------------------------------------------------------
 
5413
 *
4767
5414
 * VSockVmciStreamRecvmsg --
4768
5415
 *
4769
5416
 *    Receives a datagram and places it in the caller's msg.
4952
5599
       */
4953
5600
 
4954
5601
      if (!(flags & MSG_PEEK)) {
 
5602
         VSOCK_STATS_STREAM_CONSUME(copied);
 
5603
 
4955
5604
         /*
4956
5605
          * If the other side has shutdown for sending and there is nothing more
4957
5606
          * to read, then modify the socket state.
5022
5671
   case SOCK_STREAM:
5023
5672
      sock->ops = &vsockVmciStreamOps;
5024
5673
      break;
 
5674
   case SOCK_SEQPACKET:
 
5675
      sock->ops = &vsockVmciSeqOps;
 
5676
      break;
5025
5677
   default:
5026
5678
      return -ESOCKTNOSUPPORT;
5027
5679
   }