18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * user-mode-linux networking multicast transport
48c2ecf20Sopenharmony_ci * Copyright (C) 2001 by Harald Welte <laforge@gnumonks.org>
58c2ecf20Sopenharmony_ci * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * based on the existing uml-networking code, which is
88c2ecf20Sopenharmony_ci * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
98c2ecf20Sopenharmony_ci * James Leu (jleu@mindspring.net).
108c2ecf20Sopenharmony_ci * Copyright (C) 2001 by various other people who didn't put their name here.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
168c2ecf20Sopenharmony_ci#include "umcast.h"
178c2ecf20Sopenharmony_ci#include <net_kern.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct umcast_init {
208c2ecf20Sopenharmony_ci	char *addr;
218c2ecf20Sopenharmony_ci	int lport;
228c2ecf20Sopenharmony_ci	int rport;
238c2ecf20Sopenharmony_ci	int ttl;
248c2ecf20Sopenharmony_ci	bool unicast;
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic void umcast_init(struct net_device *dev, void *data)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct uml_net_private *pri;
308c2ecf20Sopenharmony_ci	struct umcast_data *dpri;
318c2ecf20Sopenharmony_ci	struct umcast_init *init = data;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	pri = netdev_priv(dev);
348c2ecf20Sopenharmony_ci	dpri = (struct umcast_data *) pri->user;
358c2ecf20Sopenharmony_ci	dpri->addr = init->addr;
368c2ecf20Sopenharmony_ci	dpri->lport = init->lport;
378c2ecf20Sopenharmony_ci	dpri->rport = init->rport;
388c2ecf20Sopenharmony_ci	dpri->unicast = init->unicast;
398c2ecf20Sopenharmony_ci	dpri->ttl = init->ttl;
408c2ecf20Sopenharmony_ci	dpri->dev = dev;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	if (dpri->unicast) {
438c2ecf20Sopenharmony_ci		printk(KERN_INFO "ucast backend address: %s:%u listen port: "
448c2ecf20Sopenharmony_ci		       "%u\n", dpri->addr, dpri->rport, dpri->lport);
458c2ecf20Sopenharmony_ci	} else {
468c2ecf20Sopenharmony_ci		printk(KERN_INFO "mcast backend multicast address: %s:%u, "
478c2ecf20Sopenharmony_ci		       "TTL:%u\n", dpri->addr, dpri->lport, dpri->ttl);
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int umcast_read(int fd, struct sk_buff *skb, struct uml_net_private *lp)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	return net_recvfrom(fd, skb_mac_header(skb),
548c2ecf20Sopenharmony_ci			    skb->dev->mtu + ETH_HEADER_OTHER);
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int umcast_write(int fd, struct sk_buff *skb, struct uml_net_private *lp)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	return umcast_user_write(fd, skb->data, skb->len,
608c2ecf20Sopenharmony_ci				(struct umcast_data *) &lp->user);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct net_kern_info umcast_kern_info = {
648c2ecf20Sopenharmony_ci	.init			= umcast_init,
658c2ecf20Sopenharmony_ci	.protocol		= eth_protocol,
668c2ecf20Sopenharmony_ci	.read			= umcast_read,
678c2ecf20Sopenharmony_ci	.write			= umcast_write,
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic int mcast_setup(char *str, char **mac_out, void *data)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct umcast_init *init = data;
738c2ecf20Sopenharmony_ci	char *port_str = NULL, *ttl_str = NULL, *remain;
748c2ecf20Sopenharmony_ci	char *last;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	*init = ((struct umcast_init)
778c2ecf20Sopenharmony_ci		{ .addr	= "239.192.168.1",
788c2ecf20Sopenharmony_ci		  .lport	= 1102,
798c2ecf20Sopenharmony_ci		  .ttl	= 1 });
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	remain = split_if_spec(str, mac_out, &init->addr, &port_str, &ttl_str,
828c2ecf20Sopenharmony_ci			       NULL);
838c2ecf20Sopenharmony_ci	if (remain != NULL) {
848c2ecf20Sopenharmony_ci		printk(KERN_ERR "mcast_setup - Extra garbage on "
858c2ecf20Sopenharmony_ci		       "specification : '%s'\n", remain);
868c2ecf20Sopenharmony_ci		return 0;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (port_str != NULL) {
908c2ecf20Sopenharmony_ci		init->lport = simple_strtoul(port_str, &last, 10);
918c2ecf20Sopenharmony_ci		if ((*last != '\0') || (last == port_str)) {
928c2ecf20Sopenharmony_ci			printk(KERN_ERR "mcast_setup - Bad port : '%s'\n",
938c2ecf20Sopenharmony_ci			       port_str);
948c2ecf20Sopenharmony_ci			return 0;
958c2ecf20Sopenharmony_ci		}
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (ttl_str != NULL) {
998c2ecf20Sopenharmony_ci		init->ttl = simple_strtoul(ttl_str, &last, 10);
1008c2ecf20Sopenharmony_ci		if ((*last != '\0') || (last == ttl_str)) {
1018c2ecf20Sopenharmony_ci			printk(KERN_ERR "mcast_setup - Bad ttl : '%s'\n",
1028c2ecf20Sopenharmony_ci			       ttl_str);
1038c2ecf20Sopenharmony_ci			return 0;
1048c2ecf20Sopenharmony_ci		}
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	init->unicast = false;
1088c2ecf20Sopenharmony_ci	init->rport = init->lport;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	printk(KERN_INFO "Configured mcast device: %s:%u-%u\n", init->addr,
1118c2ecf20Sopenharmony_ci	       init->lport, init->ttl);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return 1;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int ucast_setup(char *str, char **mac_out, void *data)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct umcast_init *init = data;
1198c2ecf20Sopenharmony_ci	char *lport_str = NULL, *rport_str = NULL, *remain;
1208c2ecf20Sopenharmony_ci	char *last;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	*init = ((struct umcast_init)
1238c2ecf20Sopenharmony_ci		{ .addr		= "",
1248c2ecf20Sopenharmony_ci		  .lport	= 1102,
1258c2ecf20Sopenharmony_ci		  .rport	= 1102 });
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	remain = split_if_spec(str, mac_out, &init->addr,
1288c2ecf20Sopenharmony_ci			       &lport_str, &rport_str, NULL);
1298c2ecf20Sopenharmony_ci	if (remain != NULL) {
1308c2ecf20Sopenharmony_ci		printk(KERN_ERR "ucast_setup - Extra garbage on "
1318c2ecf20Sopenharmony_ci		       "specification : '%s'\n", remain);
1328c2ecf20Sopenharmony_ci		return 0;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (lport_str != NULL) {
1368c2ecf20Sopenharmony_ci		init->lport = simple_strtoul(lport_str, &last, 10);
1378c2ecf20Sopenharmony_ci		if ((*last != '\0') || (last == lport_str)) {
1388c2ecf20Sopenharmony_ci			printk(KERN_ERR "ucast_setup - Bad listen port : "
1398c2ecf20Sopenharmony_ci			       "'%s'\n", lport_str);
1408c2ecf20Sopenharmony_ci			return 0;
1418c2ecf20Sopenharmony_ci		}
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (rport_str != NULL) {
1458c2ecf20Sopenharmony_ci		init->rport = simple_strtoul(rport_str, &last, 10);
1468c2ecf20Sopenharmony_ci		if ((*last != '\0') || (last == rport_str)) {
1478c2ecf20Sopenharmony_ci			printk(KERN_ERR "ucast_setup - Bad remote port : "
1488c2ecf20Sopenharmony_ci			       "'%s'\n", rport_str);
1498c2ecf20Sopenharmony_ci			return 0;
1508c2ecf20Sopenharmony_ci		}
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	init->unicast = true;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	printk(KERN_INFO "Configured ucast device: :%u -> %s:%u\n",
1568c2ecf20Sopenharmony_ci	       init->lport, init->addr, init->rport);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return 1;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic struct transport mcast_transport = {
1628c2ecf20Sopenharmony_ci	.list	= LIST_HEAD_INIT(mcast_transport.list),
1638c2ecf20Sopenharmony_ci	.name	= "mcast",
1648c2ecf20Sopenharmony_ci	.setup	= mcast_setup,
1658c2ecf20Sopenharmony_ci	.user	= &umcast_user_info,
1668c2ecf20Sopenharmony_ci	.kern	= &umcast_kern_info,
1678c2ecf20Sopenharmony_ci	.private_size	= sizeof(struct umcast_data),
1688c2ecf20Sopenharmony_ci	.setup_size	= sizeof(struct umcast_init),
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic struct transport ucast_transport = {
1728c2ecf20Sopenharmony_ci	.list	= LIST_HEAD_INIT(ucast_transport.list),
1738c2ecf20Sopenharmony_ci	.name	= "ucast",
1748c2ecf20Sopenharmony_ci	.setup	= ucast_setup,
1758c2ecf20Sopenharmony_ci	.user	= &umcast_user_info,
1768c2ecf20Sopenharmony_ci	.kern	= &umcast_kern_info,
1778c2ecf20Sopenharmony_ci	.private_size	= sizeof(struct umcast_data),
1788c2ecf20Sopenharmony_ci	.setup_size	= sizeof(struct umcast_init),
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic int register_umcast(void)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	register_transport(&mcast_transport);
1848c2ecf20Sopenharmony_ci	register_transport(&ucast_transport);
1858c2ecf20Sopenharmony_ci	return 0;
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cilate_initcall(register_umcast);
189