Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * user-mode-linux networking multicast transport |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 3 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * based on the existing uml-networking code, which is |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 7 | * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * James Leu (jleu@mindspring.net). |
| 9 | * Copyright (C) 2001 by various other people who didn't put their name here. |
| 10 | * |
| 11 | * Licensed under the GPL. |
| 12 | * |
| 13 | */ |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <unistd.h> |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 16 | #include <errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <netinet/in.h> |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 18 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "mcast.h" |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 20 | #include "net_user.h" |
Paolo 'Blaisorblade' Giarrusso | c13e569 | 2006-10-19 23:28:20 -0700 | [diff] [blame] | 21 | #include "um_malloc.h" |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 22 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static struct sockaddr_in *new_addr(char *addr, unsigned short port) |
| 25 | { |
| 26 | struct sockaddr_in *sin; |
| 27 | |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 28 | sin = uml_kmalloc(sizeof(struct sockaddr_in), UM_GFP_KERNEL); |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 29 | if (sin == NULL) { |
| 30 | printk(UM_KERN_ERR "new_addr: allocation of sockaddr_in " |
| 31 | "failed\n"); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 32 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } |
| 34 | sin->sin_family = AF_INET; |
| 35 | sin->sin_addr.s_addr = in_aton(addr); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 36 | sin->sin_port = htons(port); |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 37 | return sin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 40 | static int mcast_user_init(void *data, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | struct mcast_data *pri = data; |
| 43 | |
| 44 | pri->mcast_addr = new_addr(pri->addr, pri->port); |
| 45 | pri->dev = dev; |
Jeff Dike | f34d9d2 | 2007-05-06 14:51:04 -0700 | [diff] [blame] | 46 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Paolo 'Blaisorblade' Giarrusso | 83f4e8a | 2007-03-07 20:41:09 -0800 | [diff] [blame] | 49 | static void mcast_remove(void *data) |
| 50 | { |
| 51 | struct mcast_data *pri = data; |
| 52 | |
| 53 | kfree(pri->mcast_addr); |
| 54 | pri->mcast_addr = NULL; |
| 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static int mcast_open(void *data) |
| 58 | { |
| 59 | struct mcast_data *pri = data; |
| 60 | struct sockaddr_in *sin = pri->mcast_addr; |
| 61 | struct ip_mreq mreq; |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 62 | int fd, yes = 1, err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 65 | if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 69 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 70 | if (fd < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 71 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 72 | printk(UM_KERN_ERR "mcast_open : data socket failed, " |
| 73 | "errno = %d\n", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | goto out; |
| 75 | } |
| 76 | |
| 77 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 78 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 79 | printk(UM_KERN_ERR "mcast_open: SO_REUSEADDR failed, " |
| 80 | "errno = %d\n", errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 81 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* set ttl according to config */ |
| 85 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &pri->ttl, |
| 86 | sizeof(pri->ttl)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 87 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 88 | printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_TTL failed, " |
| 89 | "error = %d\n", errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 90 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* set LOOP, so data does get fed back to local sockets */ |
| 94 | if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 95 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 96 | printk(UM_KERN_ERR "mcast_open: IP_MULTICAST_LOOP failed, " |
| 97 | "error = %d\n", errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 98 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* bind socket to mcast address */ |
| 102 | if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 103 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 104 | printk(UM_KERN_ERR "mcast_open : data bind failed, " |
| 105 | "errno = %d\n", errno); |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 106 | goto out_close; |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | /* subscribe to the multicast group */ |
| 110 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; |
| 111 | mreq.imr_interface.s_addr = 0; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 112 | if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | &mreq, sizeof(mreq)) < 0) { |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 114 | err = -errno; |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 115 | printk(UM_KERN_ERR "mcast_open: IP_ADD_MEMBERSHIP failed, " |
| 116 | "error = %d\n", errno); |
| 117 | printk(UM_KERN_ERR "There appears not to be a multicast-" |
| 118 | "capable network interface on the host.\n"); |
| 119 | printk(UM_KERN_ERR "eth0 should be configured in order to use " |
| 120 | "the multicast transport.\n"); |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 121 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Dike | 7c00c31 | 2005-05-20 13:59:09 -0700 | [diff] [blame] | 124 | return fd; |
| 125 | |
| 126 | out_close: |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 127 | close(fd); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 128 | out: |
Paolo 'Blaisorblade' Giarrusso | c50d2c4 | 2005-11-13 16:07:07 -0800 | [diff] [blame] | 129 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void mcast_close(int fd, void *data) |
| 133 | { |
| 134 | struct ip_mreq mreq; |
| 135 | struct mcast_data *pri = data; |
| 136 | struct sockaddr_in *sin = pri->mcast_addr; |
| 137 | |
| 138 | mreq.imr_multiaddr.s_addr = sin->sin_addr.s_addr; |
| 139 | mreq.imr_interface.s_addr = 0; |
| 140 | if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, |
| 141 | &mreq, sizeof(mreq)) < 0) { |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 142 | printk(UM_KERN_ERR "mcast_open: IP_DROP_MEMBERSHIP failed, " |
| 143 | "error = %d\n", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jeff Dike | cd1ae0e | 2007-10-16 01:27:29 -0700 | [diff] [blame] | 146 | close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int mcast_user_write(int fd, void *buf, int len, struct mcast_data *pri) |
| 150 | { |
| 151 | struct sockaddr_in *data_addr = pri->mcast_addr; |
| 152 | |
Jeff Dike | 56bd194 | 2007-05-06 14:51:02 -0700 | [diff] [blame] | 153 | return net_sendto(fd, buf, len, data_addr, sizeof(*data_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 156 | const struct net_user_info mcast_user_info = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | .init = mcast_user_init, |
| 158 | .open = mcast_open, |
| 159 | .close = mcast_close, |
Paolo 'Blaisorblade' Giarrusso | 83f4e8a | 2007-03-07 20:41:09 -0800 | [diff] [blame] | 160 | .remove = mcast_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | .add_address = NULL, |
| 162 | .delete_address = NULL, |
Jeff Dike | b53f35a | 2007-10-16 01:27:31 -0700 | [diff] [blame] | 163 | .mtu = ETH_MAX_PACKET, |
| 164 | .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | }; |