net.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "net.h"
  26. #include "console.h"
  27. #include "sysemu.h"
  28. #include "qemu-timer.h"
  29. #include "qemu-char.h"
  30. #include "audio/audio.h"
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <signal.h>
  34. #include <time.h>
  35. #include <errno.h>
  36. #include <sys/time.h>
  37. #include <zlib.h>
  38. #ifndef _WIN32
  39. #include <sys/times.h>
  40. #include <sys/wait.h>
  41. #include <termios.h>
  42. #include <sys/mman.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/resource.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <net/if.h>
  48. #ifdef __NetBSD__
  49. #include <net/if_tap.h>
  50. #endif
  51. #ifdef __linux__
  52. #include <linux/if_tun.h>
  53. #endif
  54. #include <arpa/inet.h>
  55. #include <dirent.h>
  56. #include <netdb.h>
  57. #include <sys/select.h>
  58. #ifdef _BSD
  59. #include <sys/stat.h>
  60. #ifdef __FreeBSD__
  61. #include <libutil.h>
  62. #else
  63. #include <util.h>
  64. #endif
  65. #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
  66. #include <freebsd/stdlib.h>
  67. #else
  68. #ifdef __linux__
  69. #include <pty.h>
  70. #include <malloc.h>
  71. #include <linux/rtc.h>
  72. /* For the benefit of older linux systems which don't supply it,
  73. we use a local copy of hpet.h. */
  74. /* #include <linux/hpet.h> */
  75. #include "hpet.h"
  76. #include <linux/ppdev.h>
  77. #include <linux/parport.h>
  78. #endif
  79. #ifdef __sun__
  80. #include <sys/stat.h>
  81. #include <sys/ethernet.h>
  82. #include <sys/sockio.h>
  83. #include <netinet/arp.h>
  84. #include <netinet/in.h>
  85. #include <netinet/in_systm.h>
  86. #include <netinet/ip.h>
  87. #include <netinet/ip_icmp.h> // must come after ip.h
  88. #include <netinet/udp.h>
  89. #include <netinet/tcp.h>
  90. #include <net/if.h>
  91. #include <syslog.h>
  92. #include <stropts.h>
  93. #endif
  94. #endif
  95. #endif
  96. #include "qemu_socket.h"
  97. #if defined(CONFIG_SLIRP)
  98. #include "libslirp.h"
  99. #endif
  100. #if defined(__OpenBSD__)
  101. #include <util.h>
  102. #endif
  103. #if defined(CONFIG_VDE)
  104. #include <libvdeplug.h>
  105. #endif
  106. #ifdef _WIN32
  107. #include <malloc.h>
  108. #include <sys/timeb.h>
  109. #include <mmsystem.h>
  110. #define getopt_long_only getopt_long
  111. #define memalign(align, size) malloc(size)
  112. #endif
  113. static VLANState *first_vlan;
  114. /***********************************************************/
  115. /* network device redirectors */
  116. #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
  117. static void hex_dump(FILE *f, const uint8_t *buf, int size)
  118. {
  119. int len, i, j, c;
  120. for(i=0;i<size;i+=16) {
  121. len = size - i;
  122. if (len > 16)
  123. len = 16;
  124. fprintf(f, "%08x ", i);
  125. for(j=0;j<16;j++) {
  126. if (j < len)
  127. fprintf(f, " %02x", buf[i+j]);
  128. else
  129. fprintf(f, " ");
  130. }
  131. fprintf(f, " ");
  132. for(j=0;j<len;j++) {
  133. c = buf[i+j];
  134. if (c < ' ' || c > '~')
  135. c = '.';
  136. fprintf(f, "%c", c);
  137. }
  138. fprintf(f, "\n");
  139. }
  140. }
  141. #endif
  142. static int parse_macaddr(uint8_t *macaddr, const char *p)
  143. {
  144. int i;
  145. char *last_char;
  146. long int offset;
  147. errno = 0;
  148. offset = strtol(p, &last_char, 0);
  149. if (0 == errno && '\0' == *last_char &&
  150. offset >= 0 && offset <= 0xFFFFFF) {
  151. macaddr[3] = (offset & 0xFF0000) >> 16;
  152. macaddr[4] = (offset & 0xFF00) >> 8;
  153. macaddr[5] = offset & 0xFF;
  154. return 0;
  155. } else {
  156. for(i = 0; i < 6; i++) {
  157. macaddr[i] = strtol(p, (char **)&p, 16);
  158. if (i == 5) {
  159. if (*p != '\0')
  160. return -1;
  161. } else {
  162. if (*p != ':' && *p != '-')
  163. return -1;
  164. p++;
  165. }
  166. }
  167. return 0;
  168. }
  169. return -1;
  170. }
  171. static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
  172. {
  173. const char *p, *p1;
  174. int len;
  175. p = *pp;
  176. p1 = strchr(p, sep);
  177. if (!p1)
  178. return -1;
  179. len = p1 - p;
  180. p1++;
  181. if (buf_size > 0) {
  182. if (len > buf_size - 1)
  183. len = buf_size - 1;
  184. memcpy(buf, p, len);
  185. buf[len] = '\0';
  186. }
  187. *pp = p1;
  188. return 0;
  189. }
  190. int parse_host_src_port(struct sockaddr_in *haddr,
  191. struct sockaddr_in *saddr,
  192. const char *input_str)
  193. {
  194. char *str = strdup(input_str);
  195. char *host_str = str;
  196. char *src_str;
  197. const char *src_str2;
  198. char *ptr;
  199. /*
  200. * Chop off any extra arguments at the end of the string which
  201. * would start with a comma, then fill in the src port information
  202. * if it was provided else use the "any address" and "any port".
  203. */
  204. if ((ptr = strchr(str,',')))
  205. *ptr = '\0';
  206. if ((src_str = strchr(input_str,'@'))) {
  207. *src_str = '\0';
  208. src_str++;
  209. }
  210. if (parse_host_port(haddr, host_str) < 0)
  211. goto fail;
  212. src_str2 = src_str;
  213. if (!src_str || *src_str == '\0')
  214. src_str2 = ":0";
  215. if (parse_host_port(saddr, src_str2) < 0)
  216. goto fail;
  217. free(str);
  218. return(0);
  219. fail:
  220. free(str);
  221. return -1;
  222. }
  223. int parse_host_port(struct sockaddr_in *saddr, const char *str)
  224. {
  225. char buf[512];
  226. struct hostent *he;
  227. const char *p, *r;
  228. int port;
  229. p = str;
  230. if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
  231. return -1;
  232. saddr->sin_family = AF_INET;
  233. if (buf[0] == '\0') {
  234. saddr->sin_addr.s_addr = 0;
  235. } else {
  236. if (qemu_isdigit(buf[0])) {
  237. if (!inet_aton(buf, &saddr->sin_addr))
  238. return -1;
  239. } else {
  240. if ((he = gethostbyname(buf)) == NULL)
  241. return - 1;
  242. saddr->sin_addr = *(struct in_addr *)he->h_addr;
  243. }
  244. }
  245. port = strtol(p, (char **)&r, 0);
  246. if (r == p)
  247. return -1;
  248. saddr->sin_port = htons(port);
  249. return 0;
  250. }
  251. #if !defined(_WIN32) && 0
  252. static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
  253. {
  254. const char *p;
  255. int len;
  256. len = MIN(108, strlen(str));
  257. p = strchr(str, ',');
  258. if (p)
  259. len = MIN(len, p - str);
  260. memset(uaddr, 0, sizeof(*uaddr));
  261. uaddr->sun_family = AF_UNIX;
  262. memcpy(uaddr->sun_path, str, len);
  263. return 0;
  264. }
  265. #endif
  266. void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
  267. {
  268. snprintf(vc->info_str, sizeof(vc->info_str),
  269. "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
  270. vc->model,
  271. macaddr[0], macaddr[1], macaddr[2],
  272. macaddr[3], macaddr[4], macaddr[5]);
  273. }
  274. static char *assign_name(VLANClientState *vc1, const char *model)
  275. {
  276. VLANState *vlan;
  277. char buf[256];
  278. int id = 0;
  279. for (vlan = first_vlan; vlan; vlan = vlan->next) {
  280. VLANClientState *vc;
  281. for (vc = vlan->first_client; vc; vc = vc->next)
  282. if (vc != vc1 && strcmp(vc->model, model) == 0)
  283. id++;
  284. }
  285. snprintf(buf, sizeof(buf), "%s.%d", model, id);
  286. return strdup(buf);
  287. }
  288. VLANClientState *qemu_new_vlan_client(VLANState *vlan,
  289. const char *model,
  290. const char *name,
  291. IOReadHandler *fd_read,
  292. IOCanRWHandler *fd_can_read,
  293. NetCleanup *cleanup,
  294. void *opaque)
  295. {
  296. VLANClientState *vc, **pvc;
  297. vc = qemu_mallocz(sizeof(VLANClientState));
  298. vc->model = strdup(model);
  299. if (name)
  300. vc->name = strdup(name);
  301. else
  302. vc->name = assign_name(vc, model);
  303. vc->fd_read = fd_read;
  304. vc->fd_can_read = fd_can_read;
  305. vc->cleanup = cleanup;
  306. vc->opaque = opaque;
  307. vc->vlan = vlan;
  308. vc->next = NULL;
  309. pvc = &vlan->first_client;
  310. while (*pvc != NULL)
  311. pvc = &(*pvc)->next;
  312. *pvc = vc;
  313. return vc;
  314. }
  315. void qemu_del_vlan_client(VLANClientState *vc)
  316. {
  317. VLANClientState **pvc = &vc->vlan->first_client;
  318. while (*pvc != NULL)
  319. if (*pvc == vc) {
  320. *pvc = vc->next;
  321. if (vc->cleanup) {
  322. vc->cleanup(vc);
  323. }
  324. free(vc->name);
  325. free(vc->model);
  326. qemu_free(vc);
  327. break;
  328. } else
  329. pvc = &(*pvc)->next;
  330. }
  331. VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
  332. {
  333. VLANClientState **pvc = &vlan->first_client;
  334. while (*pvc != NULL)
  335. if ((*pvc)->opaque == opaque)
  336. return *pvc;
  337. else
  338. pvc = &(*pvc)->next;
  339. return NULL;
  340. }
  341. int qemu_can_send_packet(VLANClientState *vc1)
  342. {
  343. VLANState *vlan = vc1->vlan;
  344. VLANClientState *vc;
  345. for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
  346. if (vc != vc1) {
  347. if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
  348. return 1;
  349. }
  350. }
  351. return 0;
  352. }
  353. void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
  354. {
  355. VLANState *vlan = vc1->vlan;
  356. VLANClientState *vc;
  357. if (vc1->link_down)
  358. return;
  359. #ifdef DEBUG_NET
  360. printf("vlan %d send:\n", vlan->id);
  361. hex_dump(stdout, buf, size);
  362. #endif
  363. for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
  364. if (vc != vc1 && !vc->link_down) {
  365. vc->fd_read(vc->opaque, buf, size);
  366. }
  367. }
  368. }
  369. static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
  370. int iovcnt)
  371. {
  372. uint8_t buffer[4096];
  373. size_t offset = 0;
  374. int i;
  375. for (i = 0; i < iovcnt; i++) {
  376. size_t len;
  377. len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
  378. memcpy(buffer + offset, iov[i].iov_base, len);
  379. offset += len;
  380. }
  381. vc->fd_read(vc->opaque, buffer, offset);
  382. return offset;
  383. }
  384. static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
  385. {
  386. size_t offset = 0;
  387. int i;
  388. for (i = 0; i < iovcnt; i++)
  389. offset += iov[i].iov_len;
  390. return offset;
  391. }
  392. ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
  393. int iovcnt)
  394. {
  395. VLANState *vlan = vc1->vlan;
  396. VLANClientState *vc;
  397. ssize_t max_len = 0;
  398. if (vc1->link_down)
  399. return calc_iov_length(iov, iovcnt);
  400. for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
  401. ssize_t len = 0;
  402. if (vc == vc1)
  403. continue;
  404. if (vc->link_down)
  405. len = calc_iov_length(iov, iovcnt);
  406. if (vc->fd_readv)
  407. len = vc->fd_readv(vc->opaque, iov, iovcnt);
  408. else if (vc->fd_read)
  409. len = vc_sendv_compat(vc, iov, iovcnt);
  410. max_len = MAX(max_len, len);
  411. }
  412. return max_len;
  413. }
  414. #if defined(CONFIG_SLIRP)
  415. /* slirp network adapter */
  416. static int slirp_inited;
  417. static int slirp_restrict;
  418. static char *slirp_ip;
  419. static VLANClientState *slirp_vc;
  420. int slirp_can_output(void)
  421. {
  422. return !slirp_vc || qemu_can_send_packet(slirp_vc);
  423. }
  424. void slirp_output(const uint8_t *pkt, int pkt_len)
  425. {
  426. #ifdef DEBUG_SLIRP
  427. printf("slirp output:\n");
  428. hex_dump(stdout, pkt, pkt_len);
  429. #endif
  430. if (!slirp_vc)
  431. return;
  432. qemu_send_packet(slirp_vc, pkt, pkt_len);
  433. }
  434. int slirp_is_inited(void)
  435. {
  436. return slirp_inited;
  437. }
  438. static void slirp_receive(void *opaque, const uint8_t *buf, int size)
  439. {
  440. #ifdef DEBUG_SLIRP
  441. printf("slirp input:\n");
  442. hex_dump(stdout, buf, size);
  443. #endif
  444. slirp_input(buf, size);
  445. }
  446. static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
  447. {
  448. if (!slirp_inited) {
  449. slirp_inited = 1;
  450. slirp_init(slirp_restrict, slirp_ip);
  451. }
  452. slirp_vc = qemu_new_vlan_client(vlan, model, name,
  453. slirp_receive, NULL, NULL, NULL);
  454. slirp_vc->info_str[0] = '\0';
  455. return 0;
  456. }
  457. void net_slirp_redir(const char *redir_str)
  458. {
  459. int is_udp;
  460. char buf[256], *r;
  461. const char *p;
  462. struct in_addr guest_addr;
  463. int host_port, guest_port;
  464. if (!slirp_inited) {
  465. slirp_inited = 1;
  466. slirp_init(slirp_restrict, slirp_ip);
  467. }
  468. p = redir_str;
  469. if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
  470. goto fail;
  471. if (!strcmp(buf, "tcp")) {
  472. is_udp = 0;
  473. } else if (!strcmp(buf, "udp")) {
  474. is_udp = 1;
  475. } else {
  476. goto fail;
  477. }
  478. if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
  479. goto fail;
  480. host_port = strtol(buf, &r, 0);
  481. if (r == buf)
  482. goto fail;
  483. if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
  484. goto fail;
  485. if (buf[0] == '\0') {
  486. pstrcpy(buf, sizeof(buf), "10.0.2.15");
  487. }
  488. if (!inet_aton(buf, &guest_addr))
  489. goto fail;
  490. guest_port = strtol(p, &r, 0);
  491. if (r == p)
  492. goto fail;
  493. if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
  494. fprintf(stderr, "qemu: could not set up redirection\n");
  495. exit(1);
  496. }
  497. return;
  498. fail:
  499. fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
  500. exit(1);
  501. }
  502. #ifndef _WIN32
  503. static char smb_dir[1024];
  504. static void erase_dir(char *dir_name)
  505. {
  506. DIR *d;
  507. struct dirent *de;
  508. char filename[1024];
  509. /* erase all the files in the directory */
  510. if ((d = opendir(dir_name)) != 0) {
  511. for(;;) {
  512. de = readdir(d);
  513. if (!de)
  514. break;
  515. if (strcmp(de->d_name, ".") != 0 &&
  516. strcmp(de->d_name, "..") != 0) {
  517. snprintf(filename, sizeof(filename), "%s/%s",
  518. smb_dir, de->d_name);
  519. if (unlink(filename) != 0) /* is it a directory? */
  520. erase_dir(filename);
  521. }
  522. }
  523. closedir(d);
  524. rmdir(dir_name);
  525. }
  526. }
  527. /* automatic user mode samba server configuration */
  528. static void smb_exit(void)
  529. {
  530. erase_dir(smb_dir);
  531. }
  532. /* automatic user mode samba server configuration */
  533. void net_slirp_smb(const char *exported_dir)
  534. {
  535. char smb_conf[1024];
  536. char smb_cmdline[1024];
  537. FILE *f;
  538. if (!slirp_inited) {
  539. slirp_inited = 1;
  540. slirp_init(slirp_restrict, slirp_ip);
  541. }
  542. /* XXX: better tmp dir construction */
  543. snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
  544. if (mkdir(smb_dir, 0700) < 0) {
  545. fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
  546. exit(1);
  547. }
  548. snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
  549. f = fopen(smb_conf, "w");
  550. if (!f) {
  551. fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
  552. exit(1);
  553. }
  554. fprintf(f,
  555. "[global]\n"
  556. "private dir=%s\n"
  557. "smb ports=0\n"
  558. "socket address=127.0.0.1\n"
  559. "pid directory=%s\n"
  560. "lock directory=%s\n"
  561. "log file=%s/log.smbd\n"
  562. "smb passwd file=%s/smbpasswd\n"
  563. "security = share\n"
  564. "[qemu]\n"
  565. "path=%s\n"
  566. "read only=no\n"
  567. "guest ok=yes\n",
  568. smb_dir,
  569. smb_dir,
  570. smb_dir,
  571. smb_dir,
  572. smb_dir,
  573. exported_dir
  574. );
  575. fclose(f);
  576. atexit(smb_exit);
  577. snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
  578. SMBD_COMMAND, smb_conf);
  579. slirp_add_exec(0, smb_cmdline, 4, 139);
  580. }
  581. #endif /* !defined(_WIN32) */
  582. void do_info_slirp(void)
  583. {
  584. slirp_stats();
  585. }
  586. struct VMChannel {
  587. CharDriverState *hd;
  588. int port;
  589. } *vmchannels;
  590. static int vmchannel_can_read(void *opaque)
  591. {
  592. struct VMChannel *vmc = (struct VMChannel*)opaque;
  593. return slirp_socket_can_recv(4, vmc->port);
  594. }
  595. static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
  596. {
  597. struct VMChannel *vmc = (struct VMChannel*)opaque;
  598. slirp_socket_recv(4, vmc->port, buf, size);
  599. }
  600. #endif /* CONFIG_SLIRP */
  601. #if !defined(_WIN32)
  602. typedef struct TAPState {
  603. VLANClientState *vc;
  604. int fd;
  605. char down_script[1024];
  606. char down_script_arg[128];
  607. } TAPState;
  608. static int launch_script(const char *setup_script, const char *ifname, int fd);
  609. static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
  610. int iovcnt)
  611. {
  612. TAPState *s = opaque;
  613. ssize_t len;
  614. do {
  615. len = writev(s->fd, iov, iovcnt);
  616. } while (len == -1 && (errno == EINTR || errno == EAGAIN));
  617. return len;
  618. }
  619. static void tap_receive(void *opaque, const uint8_t *buf, int size)
  620. {
  621. TAPState *s = opaque;
  622. int ret;
  623. for(;;) {
  624. ret = write(s->fd, buf, size);
  625. if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
  626. } else {
  627. break;
  628. }
  629. }
  630. }
  631. static void tap_send(void *opaque)
  632. {
  633. TAPState *s = opaque;
  634. uint8_t buf[4096];
  635. int size;
  636. #ifdef __sun__
  637. struct strbuf sbuf;
  638. int f = 0;
  639. sbuf.maxlen = sizeof(buf);
  640. sbuf.buf = buf;
  641. size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
  642. #else
  643. size = read(s->fd, buf, sizeof(buf));
  644. #endif
  645. if (size > 0) {
  646. qemu_send_packet(s->vc, buf, size);
  647. }
  648. }
  649. static void tap_cleanup(VLANClientState *vc)
  650. {
  651. TAPState *s = vc->opaque;
  652. if (s->down_script[0])
  653. launch_script(s->down_script, s->down_script_arg, s->fd);
  654. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  655. close(s->fd);
  656. qemu_free(s);
  657. }
  658. /* fd support */
  659. static TAPState *net_tap_fd_init(VLANState *vlan,
  660. const char *model,
  661. const char *name,
  662. int fd)
  663. {
  664. TAPState *s;
  665. s = qemu_mallocz(sizeof(TAPState));
  666. s->fd = fd;
  667. s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
  668. NULL, tap_cleanup, s);
  669. s->vc->fd_readv = tap_receive_iov;
  670. qemu_set_fd_handler(s->fd, tap_send, NULL, s);
  671. snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
  672. return s;
  673. }
  674. #if defined (_BSD) || defined (__FreeBSD_kernel__)
  675. static int tap_open(char *ifname, int ifname_size)
  676. {
  677. int fd;
  678. char *dev;
  679. struct stat s;
  680. TFR(fd = open("/dev/tap", O_RDWR));
  681. if (fd < 0) {
  682. fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
  683. return -1;
  684. }
  685. fstat(fd, &s);
  686. dev = devname(s.st_rdev, S_IFCHR);
  687. pstrcpy(ifname, ifname_size, dev);
  688. fcntl(fd, F_SETFL, O_NONBLOCK);
  689. return fd;
  690. }
  691. #elif defined(__sun__)
  692. #define TUNNEWPPA (('T'<<16) | 0x0001)
  693. /*
  694. * Allocate TAP device, returns opened fd.
  695. * Stores dev name in the first arg(must be large enough).
  696. */
  697. int tap_alloc(char *dev, size_t dev_size)
  698. {
  699. int tap_fd, if_fd, ppa = -1;
  700. static int ip_fd = 0;
  701. char *ptr;
  702. static int arp_fd = 0;
  703. int ip_muxid, arp_muxid;
  704. struct strioctl strioc_if, strioc_ppa;
  705. int link_type = I_PLINK;;
  706. struct lifreq ifr;
  707. char actual_name[32] = "";
  708. memset(&ifr, 0x0, sizeof(ifr));
  709. if( *dev ){
  710. ptr = dev;
  711. while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
  712. ppa = atoi(ptr);
  713. }
  714. /* Check if IP device was opened */
  715. if( ip_fd )
  716. close(ip_fd);
  717. TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
  718. if (ip_fd < 0) {
  719. syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
  720. return -1;
  721. }
  722. TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
  723. if (tap_fd < 0) {
  724. syslog(LOG_ERR, "Can't open /dev/tap");
  725. return -1;
  726. }
  727. /* Assign a new PPA and get its unit number. */
  728. strioc_ppa.ic_cmd = TUNNEWPPA;
  729. strioc_ppa.ic_timout = 0;
  730. strioc_ppa.ic_len = sizeof(ppa);
  731. strioc_ppa.ic_dp = (char *)&ppa;
  732. if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
  733. syslog (LOG_ERR, "Can't assign new interface");
  734. TFR(if_fd = open("/dev/tap", O_RDWR, 0));
  735. if (if_fd < 0) {
  736. syslog(LOG_ERR, "Can't open /dev/tap (2)");
  737. return -1;
  738. }
  739. if(ioctl(if_fd, I_PUSH, "ip") < 0){
  740. syslog(LOG_ERR, "Can't push IP module");
  741. return -1;
  742. }
  743. if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
  744. syslog(LOG_ERR, "Can't get flags\n");
  745. snprintf (actual_name, 32, "tap%d", ppa);
  746. pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
  747. ifr.lifr_ppa = ppa;
  748. /* Assign ppa according to the unit number returned by tun device */
  749. if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
  750. syslog (LOG_ERR, "Can't set PPA %d", ppa);
  751. if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
  752. syslog (LOG_ERR, "Can't get flags\n");
  753. /* Push arp module to if_fd */
  754. if (ioctl (if_fd, I_PUSH, "arp") < 0)
  755. syslog (LOG_ERR, "Can't push ARP module (2)");
  756. /* Push arp module to ip_fd */
  757. if (ioctl (ip_fd, I_POP, NULL) < 0)
  758. syslog (LOG_ERR, "I_POP failed\n");
  759. if (ioctl (ip_fd, I_PUSH, "arp") < 0)
  760. syslog (LOG_ERR, "Can't push ARP module (3)\n");
  761. /* Open arp_fd */
  762. TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
  763. if (arp_fd < 0)
  764. syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
  765. /* Set ifname to arp */
  766. strioc_if.ic_cmd = SIOCSLIFNAME;
  767. strioc_if.ic_timout = 0;
  768. strioc_if.ic_len = sizeof(ifr);
  769. strioc_if.ic_dp = (char *)&ifr;
  770. if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
  771. syslog (LOG_ERR, "Can't set ifname to arp\n");
  772. }
  773. if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
  774. syslog(LOG_ERR, "Can't link TAP device to IP");
  775. return -1;
  776. }
  777. if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
  778. syslog (LOG_ERR, "Can't link TAP device to ARP");
  779. close (if_fd);
  780. memset(&ifr, 0x0, sizeof(ifr));
  781. pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
  782. ifr.lifr_ip_muxid = ip_muxid;
  783. ifr.lifr_arp_muxid = arp_muxid;
  784. if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
  785. {
  786. ioctl (ip_fd, I_PUNLINK , arp_muxid);
  787. ioctl (ip_fd, I_PUNLINK, ip_muxid);
  788. syslog (LOG_ERR, "Can't set multiplexor id");
  789. }
  790. snprintf(dev, dev_size, "tap%d", ppa);
  791. return tap_fd;
  792. }
  793. static int tap_open(char *ifname, int ifname_size)
  794. {
  795. char dev[10]="";
  796. int fd;
  797. if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
  798. fprintf(stderr, "Cannot allocate TAP device\n");
  799. return -1;
  800. }
  801. pstrcpy(ifname, ifname_size, dev);
  802. fcntl(fd, F_SETFL, O_NONBLOCK);
  803. return fd;
  804. }
  805. #elif defined (_AIX)
  806. static int tap_open(char *ifname, int ifname_size)
  807. {
  808. fprintf (stderr, "no tap on AIX\n");
  809. return -1;
  810. }
  811. #else
  812. static int tap_open(char *ifname, int ifname_size)
  813. {
  814. struct ifreq ifr;
  815. int fd, ret;
  816. TFR(fd = open("/dev/net/tun", O_RDWR));
  817. if (fd < 0) {
  818. fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
  819. return -1;
  820. }
  821. memset(&ifr, 0, sizeof(ifr));
  822. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  823. if (ifname[0] != '\0')
  824. pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
  825. else
  826. pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
  827. ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
  828. if (ret != 0) {
  829. fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
  830. close(fd);
  831. return -1;
  832. }
  833. pstrcpy(ifname, ifname_size, ifr.ifr_name);
  834. fcntl(fd, F_SETFL, O_NONBLOCK);
  835. return fd;
  836. }
  837. #endif
  838. static int launch_script(const char *setup_script, const char *ifname, int fd)
  839. {
  840. int pid, status;
  841. char *args[3];
  842. char **parg;
  843. /* try to launch network script */
  844. pid = fork();
  845. if (pid >= 0) {
  846. if (pid == 0) {
  847. int open_max = sysconf (_SC_OPEN_MAX), i;
  848. for (i = 0; i < open_max; i++)
  849. if (i != STDIN_FILENO &&
  850. i != STDOUT_FILENO &&
  851. i != STDERR_FILENO &&
  852. i != fd)
  853. close(i);
  854. parg = args;
  855. *parg++ = (char *)setup_script;
  856. *parg++ = (char *)ifname;
  857. *parg++ = NULL;
  858. execv(setup_script, args);
  859. _exit(1);
  860. }
  861. while (waitpid(pid, &status, 0) != pid);
  862. if (!WIFEXITED(status) ||
  863. WEXITSTATUS(status) != 0) {
  864. fprintf(stderr, "%s: could not launch network script\n",
  865. setup_script);
  866. return -1;
  867. }
  868. }
  869. return 0;
  870. }
  871. static int net_tap_init(VLANState *vlan, const char *model,
  872. const char *name, const char *ifname1,
  873. const char *setup_script, const char *down_script)
  874. {
  875. TAPState *s;
  876. int fd;
  877. char ifname[128];
  878. if (ifname1 != NULL)
  879. pstrcpy(ifname, sizeof(ifname), ifname1);
  880. else
  881. ifname[0] = '\0';
  882. TFR(fd = tap_open(ifname, sizeof(ifname)));
  883. if (fd < 0)
  884. return -1;
  885. if (!setup_script || !strcmp(setup_script, "no"))
  886. setup_script = "";
  887. if (setup_script[0] != '\0') {
  888. if (launch_script(setup_script, ifname, fd))
  889. return -1;
  890. }
  891. s = net_tap_fd_init(vlan, model, name, fd);
  892. snprintf(s->vc->info_str, sizeof(s->vc->info_str),
  893. "ifname=%s,script=%s,downscript=%s",
  894. ifname, setup_script, down_script);
  895. if (down_script && strcmp(down_script, "no")) {
  896. snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
  897. snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
  898. }
  899. return 0;
  900. }
  901. #endif /* !_WIN32 */
  902. #if defined(CONFIG_VDE)
  903. typedef struct VDEState {
  904. VLANClientState *vc;
  905. VDECONN *vde;
  906. } VDEState;
  907. static void vde_to_qemu(void *opaque)
  908. {
  909. VDEState *s = opaque;
  910. uint8_t buf[4096];
  911. int size;
  912. size = vde_recv(s->vde, buf, sizeof(buf), 0);
  913. if (size > 0) {
  914. qemu_send_packet(s->vc, buf, size);
  915. }
  916. }
  917. static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
  918. {
  919. VDEState *s = opaque;
  920. int ret;
  921. for(;;) {
  922. ret = vde_send(s->vde, buf, size, 0);
  923. if (ret < 0 && errno == EINTR) {
  924. } else {
  925. break;
  926. }
  927. }
  928. }
  929. static void vde_cleanup(VLANClientState *vc)
  930. {
  931. VDEState *s = vc->opaque;
  932. qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
  933. vde_close(s->vde);
  934. qemu_free(s);
  935. }
  936. static int net_vde_init(VLANState *vlan, const char *model,
  937. const char *name, const char *sock,
  938. int port, const char *group, int mode)
  939. {
  940. VDEState *s;
  941. char *init_group = strlen(group) ? (char *)group : NULL;
  942. char *init_sock = strlen(sock) ? (char *)sock : NULL;
  943. struct vde_open_args args = {
  944. .port = port,
  945. .group = init_group,
  946. .mode = mode,
  947. };
  948. s = qemu_mallocz(sizeof(VDEState));
  949. s->vde = vde_open(init_sock, "QEMU", &args);
  950. if (!s->vde){
  951. free(s);
  952. return -1;
  953. }
  954. s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
  955. NULL, vde_cleanup, s);
  956. qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
  957. snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
  958. sock, vde_datafd(s->vde));
  959. return 0;
  960. }
  961. #endif
  962. /* network connection */
  963. typedef struct NetSocketState {
  964. VLANClientState *vc;
  965. int fd;
  966. int state; /* 0 = getting length, 1 = getting data */
  967. unsigned int index;
  968. unsigned int packet_len;
  969. uint8_t buf[4096];
  970. struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
  971. } NetSocketState;
  972. typedef struct NetSocketListenState {
  973. VLANState *vlan;
  974. char *model;
  975. char *name;
  976. int fd;
  977. } NetSocketListenState;
  978. /* XXX: we consider we can send the whole packet without blocking */
  979. static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
  980. {
  981. NetSocketState *s = opaque;
  982. uint32_t len;
  983. len = htonl(size);
  984. send_all(s->fd, (const uint8_t *)&len, sizeof(len));
  985. send_all(s->fd, buf, size);
  986. }
  987. static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
  988. {
  989. NetSocketState *s = opaque;
  990. sendto(s->fd, buf, size, 0,
  991. (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
  992. }
  993. static void net_socket_send(void *opaque)
  994. {
  995. NetSocketState *s = opaque;
  996. int size, err;
  997. unsigned l;
  998. uint8_t buf1[4096];
  999. const uint8_t *buf;
  1000. size = recv(s->fd, buf1, sizeof(buf1), 0);
  1001. if (size < 0) {
  1002. err = socket_error();
  1003. if (err != EWOULDBLOCK)
  1004. goto eoc;
  1005. } else if (size == 0) {
  1006. /* end of connection */
  1007. eoc:
  1008. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  1009. closesocket(s->fd);
  1010. return;
  1011. }
  1012. buf = buf1;
  1013. while (size > 0) {
  1014. /* reassemble a packet from the network */
  1015. switch(s->state) {
  1016. case 0:
  1017. l = 4 - s->index;
  1018. if (l > size)
  1019. l = size;
  1020. memcpy(s->buf + s->index, buf, l);
  1021. buf += l;
  1022. size -= l;
  1023. s->index += l;
  1024. if (s->index == 4) {
  1025. /* got length */
  1026. s->packet_len = ntohl(*(uint32_t *)s->buf);
  1027. s->index = 0;
  1028. s->state = 1;
  1029. }
  1030. break;
  1031. case 1:
  1032. l = s->packet_len - s->index;
  1033. if (l > size)
  1034. l = size;
  1035. if (s->index + l <= sizeof(s->buf)) {
  1036. memcpy(s->buf + s->index, buf, l);
  1037. } else {
  1038. fprintf(stderr, "serious error: oversized packet received,"
  1039. "connection terminated.\n");
  1040. s->state = 0;
  1041. goto eoc;
  1042. }
  1043. s->index += l;
  1044. buf += l;
  1045. size -= l;
  1046. if (s->index >= s->packet_len) {
  1047. qemu_send_packet(s->vc, s->buf, s->packet_len);
  1048. s->index = 0;
  1049. s->state = 0;
  1050. }
  1051. break;
  1052. }
  1053. }
  1054. }
  1055. static void net_socket_send_dgram(void *opaque)
  1056. {
  1057. NetSocketState *s = opaque;
  1058. int size;
  1059. size = recv(s->fd, s->buf, sizeof(s->buf), 0);
  1060. if (size < 0)
  1061. return;
  1062. if (size == 0) {
  1063. /* end of connection */
  1064. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  1065. return;
  1066. }
  1067. qemu_send_packet(s->vc, s->buf, size);
  1068. }
  1069. static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
  1070. {
  1071. struct ip_mreq imr;
  1072. int fd;
  1073. int val, ret;
  1074. if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
  1075. fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
  1076. inet_ntoa(mcastaddr->sin_addr),
  1077. (int)ntohl(mcastaddr->sin_addr.s_addr));
  1078. return -1;
  1079. }
  1080. fd = socket(PF_INET, SOCK_DGRAM, 0);
  1081. if (fd < 0) {
  1082. perror("socket(PF_INET, SOCK_DGRAM)");
  1083. return -1;
  1084. }
  1085. val = 1;
  1086. ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
  1087. (const char *)&val, sizeof(val));
  1088. if (ret < 0) {
  1089. perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
  1090. goto fail;
  1091. }
  1092. ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
  1093. if (ret < 0) {
  1094. perror("bind");
  1095. goto fail;
  1096. }
  1097. /* Add host to multicast group */
  1098. imr.imr_multiaddr = mcastaddr->sin_addr;
  1099. imr.imr_interface.s_addr = htonl(INADDR_ANY);
  1100. ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1101. (const char *)&imr, sizeof(struct ip_mreq));
  1102. if (ret < 0) {
  1103. perror("setsockopt(IP_ADD_MEMBERSHIP)");
  1104. goto fail;
  1105. }
  1106. /* Force mcast msgs to loopback (eg. several QEMUs in same host */
  1107. val = 1;
  1108. ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
  1109. (const char *)&val, sizeof(val));
  1110. if (ret < 0) {
  1111. perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
  1112. goto fail;
  1113. }
  1114. socket_set_nonblock(fd);
  1115. return fd;
  1116. fail:
  1117. if (fd >= 0)
  1118. closesocket(fd);
  1119. return -1;
  1120. }
  1121. static void net_socket_cleanup(VLANClientState *vc)
  1122. {
  1123. NetSocketState *s = vc->opaque;
  1124. qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
  1125. close(s->fd);
  1126. qemu_free(s);
  1127. }
  1128. static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
  1129. const char *model,
  1130. const char *name,
  1131. int fd, int is_connected)
  1132. {
  1133. struct sockaddr_in saddr;
  1134. int newfd;
  1135. socklen_t saddr_len;
  1136. NetSocketState *s;
  1137. /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
  1138. * Because this may be "shared" socket from a "master" process, datagrams would be recv()
  1139. * by ONLY ONE process: we must "clone" this dgram socket --jjo
  1140. */
  1141. if (is_connected) {
  1142. if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
  1143. /* must be bound */
  1144. if (saddr.sin_addr.s_addr==0) {
  1145. fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
  1146. fd);
  1147. return NULL;
  1148. }
  1149. /* clone dgram socket */
  1150. newfd = net_socket_mcast_create(&saddr);
  1151. if (newfd < 0) {
  1152. /* error already reported by net_socket_mcast_create() */
  1153. close(fd);
  1154. return NULL;
  1155. }
  1156. /* clone newfd to fd, close newfd */
  1157. dup2(newfd, fd);
  1158. close(newfd);
  1159. } else {
  1160. fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
  1161. fd, strerror(errno));
  1162. return NULL;
  1163. }
  1164. }
  1165. s = qemu_mallocz(sizeof(NetSocketState));
  1166. s->fd = fd;
  1167. s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
  1168. NULL, net_socket_cleanup, s);
  1169. qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
  1170. /* mcast: save bound address as dst */
  1171. if (is_connected) s->dgram_dst=saddr;
  1172. snprintf(s->vc->info_str, sizeof(s->vc->info_str),
  1173. "socket: fd=%d (%s mcast=%s:%d)",
  1174. fd, is_connected? "cloned" : "",
  1175. inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  1176. return s;
  1177. }
  1178. static void net_socket_connect(void *opaque)
  1179. {
  1180. NetSocketState *s = opaque;
  1181. qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
  1182. }
  1183. static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
  1184. const char *model,
  1185. const char *name,
  1186. int fd, int is_connected)
  1187. {
  1188. NetSocketState *s;
  1189. s = qemu_mallocz(sizeof(NetSocketState));
  1190. s->fd = fd;
  1191. s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
  1192. NULL, net_socket_cleanup, s);
  1193. snprintf(s->vc->info_str, sizeof(s->vc->info_str),
  1194. "socket: fd=%d", fd);
  1195. if (is_connected) {
  1196. net_socket_connect(s);
  1197. } else {
  1198. qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
  1199. }
  1200. return s;
  1201. }
  1202. static NetSocketState *net_socket_fd_init(VLANState *vlan,
  1203. const char *model, const char *name,
  1204. int fd, int is_connected)
  1205. {
  1206. int so_type=-1, optlen=sizeof(so_type);
  1207. if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
  1208. (socklen_t *)&optlen)< 0) {
  1209. fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
  1210. return NULL;
  1211. }
  1212. switch(so_type) {
  1213. case SOCK_DGRAM:
  1214. return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
  1215. case SOCK_STREAM:
  1216. return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
  1217. default:
  1218. /* who knows ... this could be a eg. a pty, do warn and continue as stream */
  1219. fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
  1220. return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
  1221. }
  1222. return NULL;
  1223. }
  1224. static void net_socket_accept(void *opaque)
  1225. {
  1226. NetSocketListenState *s = opaque;
  1227. NetSocketState *s1;
  1228. struct sockaddr_in saddr;
  1229. socklen_t len;
  1230. int fd;
  1231. for(;;) {
  1232. len = sizeof(saddr);
  1233. fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
  1234. if (fd < 0 && errno != EINTR) {
  1235. return;
  1236. } else if (fd >= 0) {
  1237. break;
  1238. }
  1239. }
  1240. s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
  1241. if (!s1) {
  1242. closesocket(fd);
  1243. } else {
  1244. snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
  1245. "socket: connection from %s:%d",
  1246. inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  1247. }
  1248. }
  1249. static int net_socket_listen_init(VLANState *vlan,
  1250. const char *model,
  1251. const char *name,
  1252. const char *host_str)
  1253. {
  1254. NetSocketListenState *s;
  1255. int fd, val, ret;
  1256. struct sockaddr_in saddr;
  1257. if (parse_host_port(&saddr, host_str) < 0)
  1258. return -1;
  1259. s = qemu_mallocz(sizeof(NetSocketListenState));
  1260. fd = socket(PF_INET, SOCK_STREAM, 0);
  1261. if (fd < 0) {
  1262. perror("socket");
  1263. return -1;
  1264. }
  1265. socket_set_nonblock(fd);
  1266. /* allow fast reuse */
  1267. val = 1;
  1268. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
  1269. ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
  1270. if (ret < 0) {
  1271. perror("bind");
  1272. return -1;
  1273. }
  1274. ret = listen(fd, 0);
  1275. if (ret < 0) {
  1276. perror("listen");
  1277. return -1;
  1278. }
  1279. s->vlan = vlan;
  1280. s->model = strdup(model);
  1281. s->name = name ? strdup(name) : NULL;
  1282. s->fd = fd;
  1283. qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
  1284. return 0;
  1285. }
  1286. static int net_socket_connect_init(VLANState *vlan,
  1287. const char *model,
  1288. const char *name,
  1289. const char *host_str)
  1290. {
  1291. NetSocketState *s;
  1292. int fd, connected, ret, err;
  1293. struct sockaddr_in saddr;
  1294. if (parse_host_port(&saddr, host_str) < 0)
  1295. return -1;
  1296. fd = socket(PF_INET, SOCK_STREAM, 0);
  1297. if (fd < 0) {
  1298. perror("socket");
  1299. return -1;
  1300. }
  1301. socket_set_nonblock(fd);
  1302. connected = 0;
  1303. for(;;) {
  1304. ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
  1305. if (ret < 0) {
  1306. err = socket_error();
  1307. if (err == EINTR || err == EWOULDBLOCK) {
  1308. } else if (err == EINPROGRESS) {
  1309. break;
  1310. #ifdef _WIN32
  1311. } else if (err == WSAEALREADY) {
  1312. break;
  1313. #endif
  1314. } else {
  1315. perror("connect");
  1316. closesocket(fd);
  1317. return -1;
  1318. }
  1319. } else {
  1320. connected = 1;
  1321. break;
  1322. }
  1323. }
  1324. s = net_socket_fd_init(vlan, model, name, fd, connected);
  1325. if (!s)
  1326. return -1;
  1327. snprintf(s->vc->info_str, sizeof(s->vc->info_str),
  1328. "socket: connect to %s:%d",
  1329. inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  1330. return 0;
  1331. }
  1332. static int net_socket_mcast_init(VLANState *vlan,
  1333. const char *model,
  1334. const char *name,
  1335. const char *host_str)
  1336. {
  1337. NetSocketState *s;
  1338. int fd;
  1339. struct sockaddr_in saddr;
  1340. if (parse_host_port(&saddr, host_str) < 0)
  1341. return -1;
  1342. fd = net_socket_mcast_create(&saddr);
  1343. if (fd < 0)
  1344. return -1;
  1345. s = net_socket_fd_init(vlan, model, name, fd, 0);
  1346. if (!s)
  1347. return -1;
  1348. s->dgram_dst = saddr;
  1349. snprintf(s->vc->info_str, sizeof(s->vc->info_str),
  1350. "socket: mcast=%s:%d",
  1351. inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  1352. return 0;
  1353. }
  1354. /* find or alloc a new VLAN */
  1355. VLANState *qemu_find_vlan(int id)
  1356. {
  1357. VLANState **pvlan, *vlan;
  1358. for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
  1359. if (vlan->id == id)
  1360. return vlan;
  1361. }
  1362. vlan = qemu_mallocz(sizeof(VLANState));
  1363. vlan->id = id;
  1364. vlan->next = NULL;
  1365. pvlan = &first_vlan;
  1366. while (*pvlan != NULL)
  1367. pvlan = &(*pvlan)->next;
  1368. *pvlan = vlan;
  1369. return vlan;
  1370. }
  1371. static int nic_get_free_idx(void)
  1372. {
  1373. int index;
  1374. for (index = 0; index < MAX_NICS; index++)
  1375. if (!nd_table[index].used)
  1376. return index;
  1377. return -1;
  1378. }
  1379. void qemu_check_nic_model(NICInfo *nd, const char *model)
  1380. {
  1381. const char *models[2];
  1382. models[0] = model;
  1383. models[1] = NULL;
  1384. qemu_check_nic_model_list(nd, models, model);
  1385. }
  1386. void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
  1387. const char *default_model)
  1388. {
  1389. int i, exit_status = 0;
  1390. if (!nd->model)
  1391. nd->model = strdup(default_model);
  1392. if (strcmp(nd->model, "?") != 0) {
  1393. for (i = 0 ; models[i]; i++)
  1394. if (strcmp(nd->model, models[i]) == 0)
  1395. return;
  1396. fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
  1397. exit_status = 1;
  1398. }
  1399. fprintf(stderr, "qemu: Supported NIC models: ");
  1400. for (i = 0 ; models[i]; i++)
  1401. fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
  1402. exit(exit_status);
  1403. }
  1404. int net_client_init(const char *device, const char *p)
  1405. {
  1406. char buf[1024];
  1407. int vlan_id, ret;
  1408. VLANState *vlan;
  1409. char *name = NULL;
  1410. vlan_id = 0;
  1411. if (get_param_value(buf, sizeof(buf), "vlan", p)) {
  1412. vlan_id = strtol(buf, NULL, 0);
  1413. }
  1414. vlan = qemu_find_vlan(vlan_id);
  1415. if (get_param_value(buf, sizeof(buf), "name", p)) {
  1416. name = strdup(buf);
  1417. }
  1418. if (!strcmp(device, "nic")) {
  1419. NICInfo *nd;
  1420. uint8_t *macaddr;
  1421. int idx = nic_get_free_idx();
  1422. if (idx == -1 || nb_nics >= MAX_NICS) {
  1423. fprintf(stderr, "Too Many NICs\n");
  1424. ret = -1;
  1425. goto out;
  1426. }
  1427. nd = &nd_table[idx];
  1428. macaddr = nd->macaddr;
  1429. macaddr[0] = 0x52;
  1430. macaddr[1] = 0x54;
  1431. macaddr[2] = 0x00;
  1432. macaddr[3] = 0x12;
  1433. macaddr[4] = 0x34;
  1434. macaddr[5] = 0x56 + idx;
  1435. if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
  1436. if (parse_macaddr(macaddr, buf) < 0) {
  1437. fprintf(stderr, "invalid syntax for ethernet address\n");
  1438. ret = -1;
  1439. goto out;
  1440. }
  1441. }
  1442. if (get_param_value(buf, sizeof(buf), "model", p)) {
  1443. nd->model = strdup(buf);
  1444. }
  1445. nd->vlan = vlan;
  1446. nd->name = name;
  1447. nd->used = 1;
  1448. name = NULL;
  1449. nb_nics++;
  1450. vlan->nb_guest_devs++;
  1451. ret = idx;
  1452. } else
  1453. if (!strcmp(device, "none")) {
  1454. /* does nothing. It is needed to signal that no network cards
  1455. are wanted */
  1456. ret = 0;
  1457. } else
  1458. #ifdef CONFIG_SLIRP
  1459. if (!strcmp(device, "user")) {
  1460. if (get_param_value(buf, sizeof(buf), "hostname", p)) {
  1461. pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
  1462. }
  1463. if (get_param_value(buf, sizeof(buf), "restrict", p)) {
  1464. slirp_restrict = (buf[0] == 'y') ? 1 : 0;
  1465. }
  1466. if (get_param_value(buf, sizeof(buf), "ip", p)) {
  1467. slirp_ip = strdup(buf);
  1468. }
  1469. vlan->nb_host_devs++;
  1470. ret = net_slirp_init(vlan, device, name);
  1471. } else if (!strcmp(device, "channel")) {
  1472. long port;
  1473. char name[20], *devname;
  1474. struct VMChannel *vmc;
  1475. port = strtol(p, &devname, 10);
  1476. devname++;
  1477. if (port < 1 || port > 65535) {
  1478. fprintf(stderr, "vmchannel wrong port number\n");
  1479. ret = -1;
  1480. goto out;
  1481. }
  1482. vmc = malloc(sizeof(struct VMChannel));
  1483. snprintf(name, 20, "vmchannel%ld", port);
  1484. vmc->hd = qemu_chr_open(name, devname, NULL);
  1485. if (!vmc->hd) {
  1486. fprintf(stderr, "qemu: could not open vmchannel device"
  1487. "'%s'\n", devname);
  1488. ret = -1;
  1489. goto out;
  1490. }
  1491. vmc->port = port;
  1492. slirp_add_exec(3, vmc->hd, 4, port);
  1493. qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
  1494. NULL, vmc);
  1495. ret = 0;
  1496. } else
  1497. #endif
  1498. #ifdef _WIN32
  1499. if (!strcmp(device, "tap")) {
  1500. char ifname[64];
  1501. if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
  1502. fprintf(stderr, "tap: no interface name\n");
  1503. ret = -1;
  1504. goto out;
  1505. }
  1506. vlan->nb_host_devs++;
  1507. ret = tap_win32_init(vlan, device, name, ifname);
  1508. } else
  1509. #elif defined (_AIX)
  1510. #else
  1511. if (!strcmp(device, "tap")) {
  1512. char ifname[64];
  1513. char setup_script[1024], down_script[1024];
  1514. int fd;
  1515. vlan->nb_host_devs++;
  1516. if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
  1517. fd = strtol(buf, NULL, 0);
  1518. fcntl(fd, F_SETFL, O_NONBLOCK);
  1519. net_tap_fd_init(vlan, device, name, fd);
  1520. ret = 0;
  1521. } else {
  1522. if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
  1523. ifname[0] = '\0';
  1524. }
  1525. if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
  1526. pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
  1527. }
  1528. if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
  1529. pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
  1530. }
  1531. ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
  1532. }
  1533. } else
  1534. #endif
  1535. if (!strcmp(device, "socket")) {
  1536. if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
  1537. int fd;
  1538. fd = strtol(buf, NULL, 0);
  1539. ret = -1;
  1540. if (net_socket_fd_init(vlan, device, name, fd, 1))
  1541. ret = 0;
  1542. } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
  1543. ret = net_socket_listen_init(vlan, device, name, buf);
  1544. } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
  1545. ret = net_socket_connect_init(vlan, device, name, buf);
  1546. } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
  1547. ret = net_socket_mcast_init(vlan, device, name, buf);
  1548. } else {
  1549. fprintf(stderr, "Unknown socket options: %s\n", p);
  1550. ret = -1;
  1551. goto out;
  1552. }
  1553. vlan->nb_host_devs++;
  1554. } else
  1555. #ifdef CONFIG_VDE
  1556. if (!strcmp(device, "vde")) {
  1557. char vde_sock[1024], vde_group[512];
  1558. int vde_port, vde_mode;
  1559. vlan->nb_host_devs++;
  1560. if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
  1561. vde_sock[0] = '\0';
  1562. }
  1563. if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
  1564. vde_port = strtol(buf, NULL, 10);
  1565. } else {
  1566. vde_port = 0;
  1567. }
  1568. if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
  1569. vde_group[0] = '\0';
  1570. }
  1571. if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
  1572. vde_mode = strtol(buf, NULL, 8);
  1573. } else {
  1574. vde_mode = 0700;
  1575. }
  1576. ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
  1577. } else
  1578. #endif
  1579. {
  1580. fprintf(stderr, "Unknown network device: %s\n", device);
  1581. ret = -1;
  1582. goto out;
  1583. }
  1584. if (ret < 0) {
  1585. fprintf(stderr, "Could not initialize device '%s'\n", device);
  1586. }
  1587. out:
  1588. if (name)
  1589. free(name);
  1590. return ret;
  1591. }
  1592. void net_client_uninit(NICInfo *nd)
  1593. {
  1594. nd->vlan->nb_guest_devs--;
  1595. nb_nics--;
  1596. nd->used = 0;
  1597. free((void *)nd->model);
  1598. }
  1599. static int net_host_check_device(const char *device)
  1600. {
  1601. int i;
  1602. const char *valid_param_list[] = { "tap", "socket"
  1603. #ifdef CONFIG_SLIRP
  1604. ,"user"
  1605. #endif
  1606. #ifdef CONFIG_VDE
  1607. ,"vde"
  1608. #endif
  1609. };
  1610. for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
  1611. if (!strncmp(valid_param_list[i], device,
  1612. strlen(valid_param_list[i])))
  1613. return 1;
  1614. }
  1615. return 0;
  1616. }
  1617. void net_host_device_add(const char *device, const char *opts)
  1618. {
  1619. if (!net_host_check_device(device)) {
  1620. term_printf("invalid host network device %s\n", device);
  1621. return;
  1622. }
  1623. net_client_init(device, opts);
  1624. }
  1625. void net_host_device_remove(int vlan_id, const char *device)
  1626. {
  1627. VLANState *vlan;
  1628. VLANClientState *vc;
  1629. vlan = qemu_find_vlan(vlan_id);
  1630. for(vc = vlan->first_client; vc != NULL; vc = vc->next)
  1631. if (!strcmp(vc->name, device))
  1632. break;
  1633. if (!vc) {
  1634. term_printf("can't find device %s\n", device);
  1635. return;
  1636. }
  1637. qemu_del_vlan_client(vc);
  1638. }
  1639. int net_client_parse(const char *str)
  1640. {
  1641. const char *p;
  1642. char *q;
  1643. char device[64];
  1644. p = str;
  1645. q = device;
  1646. while (*p != '\0' && *p != ',') {
  1647. if ((q - device) < sizeof(device) - 1)
  1648. *q++ = *p;
  1649. p++;
  1650. }
  1651. *q = '\0';
  1652. if (*p == ',')
  1653. p++;
  1654. return net_client_init(device, p);
  1655. }
  1656. void do_info_network(void)
  1657. {
  1658. VLANState *vlan;
  1659. VLANClientState *vc;
  1660. for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
  1661. term_printf("VLAN %d devices:\n", vlan->id);
  1662. for(vc = vlan->first_client; vc != NULL; vc = vc->next)
  1663. term_printf(" %s: %s\n", vc->name, vc->info_str);
  1664. }
  1665. }
  1666. int do_set_link(const char *name, const char *up_or_down)
  1667. {
  1668. VLANState *vlan;
  1669. VLANClientState *vc = NULL;
  1670. for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
  1671. for (vc = vlan->first_client; vc != NULL; vc = vc->next)
  1672. if (strcmp(vc->name, name) == 0)
  1673. goto done;
  1674. done:
  1675. if (!vc) {
  1676. term_printf("could not find network device '%s'", name);
  1677. return 0;
  1678. }
  1679. if (strcmp(up_or_down, "up") == 0)
  1680. vc->link_down = 0;
  1681. else if (strcmp(up_or_down, "down") == 0)
  1682. vc->link_down = 1;
  1683. else
  1684. term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
  1685. up_or_down);
  1686. if (vc->link_status_changed)
  1687. vc->link_status_changed(vc);
  1688. return 1;
  1689. }
  1690. void net_cleanup(void)
  1691. {
  1692. VLANState *vlan;
  1693. /* close network clients */
  1694. for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
  1695. VLANClientState *vc = vlan->first_client;
  1696. while (vc) {
  1697. VLANClientState *next = vc->next;
  1698. qemu_del_vlan_client(vc);
  1699. vc = next;
  1700. }
  1701. }
  1702. }
  1703. void net_client_check(void)
  1704. {
  1705. VLANState *vlan;
  1706. for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
  1707. if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
  1708. continue;
  1709. if (vlan->nb_guest_devs == 0)
  1710. fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
  1711. if (vlan->nb_host_devs == 0)
  1712. fprintf(stderr,
  1713. "Warning: vlan %d is not connected to host network\n",
  1714. vlan->id);
  1715. }
  1716. }