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