54 #include <linux/capability.h>
55 #include <linux/module.h>
56 #include <linux/if_arp.h>
59 #include <linux/slab.h>
65 #include <linux/atalk.h>
69 static const struct proto_ops atalk_dgram_ops;
80 static inline void __atalk_insert_socket(
struct sock *
sk)
82 sk_add_node(sk, &atalk_sockets);
85 static inline void atalk_remove_socket(
struct sock *
sk)
140 static struct sock *atalk_find_or_insert_socket(
struct sock *
sk,
157 __atalk_insert_socket(sk);
163 static void atalk_destroy_timer(
unsigned long data)
165 struct sock *sk = (
struct sock *)data;
167 if (sk_has_allocations(sk)) {
174 static inline void atalk_destroy_socket(
struct sock *sk)
176 atalk_remove_socket(sk);
179 if (sk_has_allocations(sk)) {
215 while ((tmp = *iface) !=
NULL) {
216 if (tmp->
dev == dev) {
243 atalk_interfaces = iface;
250 static int atif_probe_device(
struct atalk_iface *atif)
252 int netrange =
ntohs(atif->
nets.nr_lastnet) -
255 int probe_node = atif->
address.s_node;
260 probe_net =
ntohs(atif->
nets.nr_firstnet);
262 probe_net +=
jiffies % netrange;
269 for (netct = 0; netct <= netrange; netct++) {
272 for (nodect = 0; nodect < 256; nodect++) {
273 atif->
address.s_node = (nodect + probe_node) & 0xFF;
274 if (atif->
address.s_node > 0 &&
287 if (probe_net >
ntohs(atif->
nets.nr_lastnet))
288 probe_net =
ntohs(atif->
nets.nr_firstnet);
297 static int atif_proxy_probe_device(
struct atalk_iface *atif,
300 int netrange =
ntohs(atif->
nets.nr_lastnet) -
309 probe_net =
ntohs(atif->
nets.nr_firstnet);
311 probe_net +=
jiffies % netrange;
318 for (netct = 0; netct <= netrange; netct++) {
321 for (nodect = 0; nodect < 256; nodect++) {
322 proxy_addr->
s_node = (nodect + probe_node) & 0xFF;
323 if (proxy_addr->
s_node > 0 &&
324 proxy_addr->
s_node < 254) {
334 if (probe_net >
ntohs(atif->
nets.nr_lastnet))
335 probe_net =
ntohs(atif->
nets.nr_firstnet);
348 static struct atalk_addr *atalk_find_primary(
void)
359 for (iface = atalk_interfaces; iface; iface = iface->
next) {
370 else if (atalk_interfaces)
371 retval = &atalk_interfaces->
address;
391 iface->
address.s_node != node &&
407 for (iface = atalk_interfaces; iface; iface = iface->
next) {
410 iface->
address.s_node == node) &&
442 for (r = atalk_routes;
r; r = r->
next) {
490 static void atrtr_set_default(
struct net_device *dev)
523 for (rt = atalk_routes; rt; rt = rt->
next) {
539 for (iface = atalk_interfaces; iface; iface = iface->
next) {
541 ntohs(ga->sat_addr.s_net) >=
543 ntohs(ga->sat_addr.s_net) <=
547 if (ga->sat_addr.s_net == iface->
address.s_net &&
548 ga->sat_addr.s_node == iface->
address.s_node)
557 devhint = riface->
dev;
593 while ((tmp = *r) !=
NULL) {
614 static void atrtr_device_down(
struct net_device *dev)
620 while ((tmp = *r) !=
NULL) {
621 if (tmp->
dev == dev) {
631 atrtr_set_default(
NULL);
635 static inline void atalk_dev_down(
struct net_device *dev)
637 atrtr_device_down(dev);
639 atif_drop_device(dev);
651 if (!net_eq(dev_net(dev), &
init_net))
663 static int atif_ioctl(
int cmd,
void __user *
arg)
665 static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
684 atif = atalk_find_dev(dev);
707 atalk_find_interface(sa->
sat_addr.s_net,
710 "interface added with "
711 "existing address\n");
731 atrtr_device_down(dev);
733 atif = atif_add_device(dev, &sa->
sat_addr);
746 atif_probe_device(atif) < 0) {
747 atif_drop_device(dev);
768 atrtr_create(&rtdef, dev);
769 atrtr_set_default(dev);
781 atrtr_create(&rtdef, dev);
851 if (atif_proxy_probe_device(atif, &(sa->
sat_addr)) < 0)
879 static int atrtr_ioctl(
unsigned int cmd,
void __user *arg)
891 &rt.rt_dst)->sat_addr);
904 return atrtr_create(&rt, dev);
921 static unsigned long atalk_sum_partial(
const unsigned char *data,
922 int len,
unsigned long sum)
934 int len,
unsigned long sum)
936 int start = skb_headlen(skb);
941 if ( (copy = start - offset) > 0) {
944 sum = atalk_sum_partial(skb->
data + offset, copy, sum);
945 if ( (len -= copy) == 0)
952 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
957 end = start + skb_frag_size(frag);
958 if ((copy = end - offset) > 0) {
964 sum = atalk_sum_partial(vaddr + frag->
page_offset +
965 offset - start, copy, sum);
975 skb_walk_frags(skb, frag_iter) {
980 end = start + frag_iter->
len;
981 if ((copy = end - offset) > 0) {
984 sum = atalk_sum_skb(frag_iter, offset - start,
986 if ((len -= copy) == 0)
998 static __be16 atalk_checksum(
const struct sk_buff *skb,
int len)
1003 sum = atalk_sum_skb(skb, 4, len-4, 0);
1006 return sum ?
htons((
unsigned short)sum) : htons(0xFFFF);
1009 static struct proto ddp_proto = {
1039 sock->
ops = &atalk_dgram_ops;
1049 static int atalk_release(
struct socket *sock)
1051 struct sock *sk = sock->
sk;
1059 atalk_destroy_socket(sk);
1077 static int atalk_pick_and_bind_port(
struct sock *sk,
struct sockaddr_at *sat)
1099 __atalk_insert_socket(sk);
1100 at_sk(sk)->src_port = sat->
sat_port;
1113 static int atalk_autobind(
struct sock *sk)
1117 struct atalk_addr *ap = atalk_find_primary();
1126 n = atalk_pick_and_bind_port(sk, &sat);
1137 struct sock *sk = sock->
sk;
1150 struct atalk_addr *ap = atalk_find_primary();
1160 if (!atalk_find_interface(addr->
sat_addr.s_net,
1169 err = atalk_pick_and_bind_port(sk, addr);
1177 if (atalk_find_or_insert_socket(sk, addr))
1189 static int atalk_connect(
struct socket *sock,
struct sockaddr *uaddr,
1190 int addr_len,
int flags)
1192 struct sock *sk = sock->
sk;
1200 if (addr_len !=
sizeof(*addr))
1211 pr_warn(
"atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
1221 if (atalk_autobind(sk) < 0)
1244 static int atalk_getname(
struct socket *sock,
struct sockaddr *uaddr,
1245 int *uaddr_len,
int peer)
1248 struct sock *sk = sock->
sk;
1255 if (atalk_autobind(sk) < 0)
1277 memcpy(uaddr, &sat,
sizeof(sat));
1284 #if defined(CONFIG_IPDDP) || defined(CONFIG_IPDDP_MODULE)
1287 return skb->
data[12] == 22;
1304 skb_reset_transport_header(skb);
1306 stats = netdev_priv(dev);
1313 #define is_ip_over_ddp(skb) 0
1314 #define handle_ip_over_ddp(skb) 0
1336 "packet received from PPP iface\n");
1344 rt = atrtr_find(&ta);
1346 len_hops += 1 << 10;
1347 if (!rt || !(len_hops & (15 << 10)))
1364 (rt->
dev->hard_header_len +
1381 if (skb_headroom(skb) < 22) {
1427 if (!net_eq(dev_net(dev), &
init_net))
1431 if (!(skb = skb_share_check(skb,
GFP_ATOMIC)))
1435 if (!pskb_may_pull(skb,
sizeof(*ddp)))
1451 if (skb->
len <
sizeof(*ddp) || skb->
len < (len_hops & 1023)) {
1452 pr_debug(
"AppleTalk: dropping corrupted frame (deh_len=%u, "
1453 "skb->len=%u)\n", len_hops & 1023, skb->
len);
1462 atalk_checksum(skb, len_hops & 1023) != ddp->
deh_sum)
1468 atif = atalk_find_anynet(ddp->
deh_dnode, dev);
1476 return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
1486 tosat.sat_addr.s_net = ddp->
deh_dnet;
1490 sock = atalk_search_socket(&tosat, atif);
1517 if (!net_eq(dev_net(dev), &
init_net))
1521 if (skb_mac_header(skb)[2] == 1) {
1526 if (!ap || skb->
len <
sizeof(
__be16) || skb->
len > 1023)
1530 if (!(skb = skb_share_check(skb,
GFP_ATOMIC)))
1547 ddp->
deh_dnode = skb_mac_header(skb)[0];
1548 ddp->
deh_snode = skb_mac_header(skb)[1];
1559 skb_reset_transport_header(skb);
1561 return atalk_rcv(skb, dev, pt, orig_dev);
1570 struct sock *sk = sock->
sk;
1593 if (atalk_autobind(sk) < 0)
1611 usat = &local_satalk;
1632 rt = atrtr_find(&at_hint);
1640 SOCK_DEBUG(sk,
"SK %p: Size needed %d, device %s\n",
1666 SOCK_DEBUG(sk,
"SK %p: Copy user data (%Zd bytes).\n", sk, len);
1678 ddp->
deh_sum = atalk_checksum(skb, len +
sizeof(*ddp));
1690 SOCK_DEBUG(sk,
"SK %p: send out(copy).\n", sk);
1708 rt = atrtr_find(&at_lo);
1730 SOCK_DEBUG(sk,
"SK %p: Done write (%Zd).\n", sk, len);
1737 static int atalk_recvmsg(
struct kiocb *iocb,
struct socket *sock,
struct msghdr *msg,
1738 size_t size,
int flags)
1740 struct sock *sk = sock->
sk;
1760 offset =
sizeof(*ddp);
1764 if (copied > size) {
1784 return err ? : copied;
1791 static int atalk_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1794 struct sock *sk = sock->
sk;
1800 long amount = sk->
sk_sndbuf - sk_wmem_alloc_get(sk);
1804 rc =
put_user(amount, (
int __user *)argp);
1817 rc =
put_user(amount, (
int __user *)argp);
1831 rc = atrtr_ioctl(cmd, argp);
1842 rc = atif_ioctl(cmd, argp);
1851 #ifdef CONFIG_COMPAT
1852 static int atalk_compat_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1861 return atalk_ioctl(sock, cmd, (
unsigned long)compat_ptr(arg));
1870 .create = atalk_create,
1874 static const struct proto_ops atalk_dgram_ops = {
1877 .release = atalk_release,
1879 .connect = atalk_connect,
1882 .getname = atalk_getname,
1884 .ioctl = atalk_ioctl,
1885 #ifdef CONFIG_COMPAT
1886 .compat_ioctl = atalk_compat_ioctl,
1892 .sendmsg = atalk_sendmsg,
1893 .recvmsg = atalk_recvmsg,
1899 .notifier_call = ddp_device_event,
1907 static struct packet_type ppptalk_packet_type __read_mostly = {
1912 static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1919 KERN_CRIT "Unable to register DDP with SNAP.\n";
1922 static int __init atalk_init(
void)
1955 static void __exit atalk_exit(
void)
1957 #ifdef CONFIG_SYSCTL