31 #include <linux/capability.h>
32 #include <linux/errno.h>
33 #include <linux/if_arp.h>
34 #include <linux/if_ether.h>
37 #include <linux/kernel.h>
38 #include <linux/list.h>
39 #include <linux/module.h>
40 #include <linux/net.h>
41 #include <linux/netdevice.h>
42 #include <linux/uio.h>
43 #include <linux/slab.h>
45 #include <linux/socket.h>
47 #include <linux/string.h>
48 #include <linux/types.h>
57 #include <asm/uaccess.h>
63 #define ipx_register_sysctl()
64 #define ipx_unregister_sysctl()
68 static unsigned char ipxcfg_max_hops = 16;
69 static char ipxcfg_auto_select_primary;
70 static char ipxcfg_auto_create_interfaces;
79 static const struct proto_ops ipx_dgram_ops;
91 struct iovec *iov,
size_t len,
int noblock);
106 static void ipxcfg_set_auto_select(
char val)
108 ipxcfg_auto_select_primary =
val;
109 if (val && !ipx_primary_net)
129 static void ipx_remove_socket(
struct sock *
sk)
139 sk_del_node_init(sk);
146 static void ipx_destroy_socket(
struct sock *sk)
148 ipx_remove_socket(sk);
160 static void ipxitf_clear_primary_net(
void)
162 ipx_primary_net =
NULL;
163 if (ipxcfg_auto_select_primary)
185 spin_lock_bh(&ipx_interfaces_lock);
186 i = __ipxitf_find_using_phys(dev, datalink);
189 spin_unlock_bh(&ipx_interfaces_lock);
220 ipx_sk(sk)->intrfc = intrfc;
234 if (ipx_sk(s)->port == port)
247 spin_lock_bh(&intrfc->if_sklist_lock);
248 s = __ipxitf_find_socket(intrfc, port);
251 spin_unlock_bh(&intrfc->if_sklist_lock);
256 #ifdef CONFIG_IPX_INTERN
258 unsigned char *ipx_node,
270 if (ipxs->
port == port &&
309 if (intrfc == ipx_primary_net)
310 ipxitf_clear_primary_net();
311 if (intrfc == ipx_internal_net)
312 ipx_internal_net =
NULL;
322 __ipxitf_down(intrfc);
329 __ipxitf_down(intrfc);
338 if (!net_eq(dev_net(dev), &
init_net))
344 spin_lock_bh(&ipx_interfaces_lock);
352 spin_unlock_bh(&ipx_interfaces_lock);
358 static __exit void ipxitf_cleanup(
void)
362 spin_lock_bh(&ipx_interfaces_lock);
365 spin_unlock_bh(&ipx_interfaces_lock);
380 #ifdef CONFIG_IPX_INTERN
384 struct ipxhdr *ipx = ipx_hdr(skb);
396 if (ipxs->
port == ipx->ipx_dest.sock &&
397 (is_broadcast || !
memcmp(ipx->ipx_dest.node,
411 ipxitf_def_skb_handler(s, skb1);
414 if (intrfc != ipx_internal_net)
429 static struct sock *ncp_connection_hack(
struct ipx_interface *intrfc,
439 struct sock *sk =
NULL;
441 u8 *ncphdr = (
u8 *)(ipx + 1);
443 if (*ncphdr == 0x22 && *(ncphdr + 1) == 0x22)
444 connection = (((
int) *(ncphdr + 5)) << 8) | (
int) *(ncphdr + 3);
445 else if (*ncphdr == 0x77 && *(ncphdr + 1) == 0x77)
446 connection = (((
int) *(ncphdr + 9)) << 8) | (
int) *(ncphdr + 8);
455 if (ipx_sk(sk)->ipx_ncp_conn == connection) {
469 struct ipxhdr *ipx = ipx_hdr(skb);
470 struct sock *sock1 =
NULL, *sock2 =
NULL;
474 if (intrfc == ipx_primary_net &&
ntohs(ipx->ipx_dest.sock) == 0x451)
475 sock1 = ncp_connection_hack(intrfc, ipx);
478 sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
488 if (ipx_primary_net && intrfc != ipx_primary_net) {
489 const int dsock =
ntohs(ipx->ipx_dest.sock);
491 if (dsock == 0x452 || dsock == 0x453 || dsock == 0x456)
496 sock2 = ipxitf_find_socket(ipx_primary_net,
504 if (!sock1 && !sock2) {
534 ipxitf_def_skb_handler(sock1, skb1);
540 ipxitf_def_skb_handler(sock2, skb2);
557 int in_offset = (
unsigned char *)ipx_hdr(skb) - skb->
head;
562 if (in_offset >= out_offset)
566 len = skb->
len + out_offset;
569 skb_reserve(skb2, out_offset);
570 skb_reset_network_header(skb2);
571 skb_reset_transport_header(skb2);
573 memcpy(ipx_hdr(skb2), ipx_hdr(skb), skb->
len);
574 memcpy(skb2->cb, skb->cb,
sizeof(skb->cb));
583 struct ipxhdr *ipx = ipx_hdr(skb);
587 int send_to_wire = 1;
591 ipx->ipx_dest.net =
IPX_SKB_CB(skb)->ipx_dest_net;
592 ipx->ipx_source.net =
IPX_SKB_CB(skb)->ipx_source_net;
598 IPX_SKB_CB(skb)->last_hop.index *
619 if (ipx->ipx_dest.net == intrfc->
if_netnum) {
624 if (intrfc == ipx_internal_net ||
648 if (ipx->ipx_source.net != intrfc->
if_netnum) {
673 skb = ipxitf_adjust_skbuff(intrfc, skb);
682 dl->
request(dl, skb, dest_node);
687 static int ipxitf_add_local_route(
struct ipx_interface *intrfc)
692 static void ipxitf_discover_netnum(
struct ipx_interface *intrfc,
698 struct ipxhdr *ipx = ipx_hdr(skb);
705 ipxitf_discover_netnum(intrfc, skb);
709 rc = ipxitf_pprop(intrfc, skb);
750 static void ipxitf_discover_netnum(
struct ipx_interface *intrfc,
764 ipxitf_add_local_route(intrfc);
767 "%lx\n %s %s and %s %s\n",
804 struct ipxhdr *ipx = ipx_hdr(skb);
828 c = ((
u8 *) ipx) +
sizeof(
struct ipxhdr);
832 for (i = 0; i <
IPX_SKB_CB(skb)->ipx_tctrl; i++)
842 spin_lock_bh(&ipx_interfaces_lock);
854 for (i = 0; i <
IPX_SKB_CB(skb)->ipx_tctrl; i++)
866 spin_unlock_bh(&ipx_interfaces_lock);
873 spin_lock_bh(&ipx_interfaces_lock);
875 spin_unlock_bh(&ipx_interfaces_lock);
877 if (ipxcfg_auto_select_primary && !ipx_primary_net)
878 ipx_primary_net = intrfc;
884 unsigned char internal,
924 intrfc = ipxitf_alloc(NULL, idef->
ipx_network, 0, NULL, 1, 0);
929 ipx_internal_net = ipx_primary_net = intrfc;
931 ipxitf_insert(intrfc);
933 rc = ipxitf_add_local_route(intrfc);
939 static __be16 ipx_map_frame_type(
unsigned char type)
962 rc = ipxitf_create_internal(idef);
988 datalink = p8022_datalink;
993 datalink = pEII_datalink;
999 datalink = pSNAP_datalink;
1003 datalink = p8023_datalink;
1020 intrfc = ipxitf_find_using_phys(dev, dlink_type);
1023 intrfc = ipxitf_alloc(dev, idef->
ipx_network, dlink_type,
1031 ipx_primary_net = intrfc;
1039 ipxitf_hold(intrfc);
1040 ipxitf_insert(intrfc);
1049 rc = ipxitf_add_local_route(intrfc);
1066 spin_lock_bh(&ipx_interfaces_lock);
1068 if (ipx_internal_net) {
1069 __ipxitf_put(ipx_internal_net);
1086 intrfc = __ipxitf_find_using_phys(dev, dlink_type);
1090 __ipxitf_put(intrfc);
1094 spin_unlock_bh(&ipx_interfaces_lock);
1111 switch (
ntohs(dlink_type)) {
1112 case ETH_P_IPX: datalink = pEII_datalink;
break;
1113 case ETH_P_802_2: datalink = p8022_datalink;
break;
1114 case ETH_P_SNAP: datalink = pSNAP_datalink;
break;
1115 case ETH_P_802_3: datalink = p8023_datalink;
break;
1119 intrfc = ipxitf_alloc(dev, 0, dlink_type, datalink, 0,
1128 ipxitf_insert(intrfc);
1136 static int ipxitf_ioctl(
unsigned int cmd,
void __user *arg)
1155 memcpy(
f.ipx_device, ifr.ifr_name,
1156 sizeof(
f.ipx_device));
1159 f.ipx_special = sipx->sipx_special;
1162 rc = ipxitf_delete(&
f);
1164 rc = ipxitf_create(&
f);
1180 ipxif = ipxitf_find_using_phys(dev,
1199 if (
get_user(val, (
unsigned char __user *) arg))
1202 ipxcfg_auto_create_interfaces =
val;
1206 if (
get_user(val, (
unsigned char __user *) arg))
1209 ipxcfg_set_auto_select(val);
1234 __u32 i = (length >> 1) - 3;
1242 if (packet->ipx_pktsize &
htons(1))
1246 sum = (sum & 0xffff) + (sum >> 16);
1266 switch (
ntohs(frame)) {
1285 static int ipx_setsockopt(
struct socket *sock,
int level,
int optname,
1286 char __user *optval,
unsigned int optlen)
1288 struct sock *sk = sock->
sk;
1293 if (optlen !=
sizeof(
int))
1297 if (
get_user(opt, (
unsigned int __user *)optval))
1304 ipx_sk(sk)->type =
opt;
1311 static int ipx_getsockopt(
struct socket *sock,
int level,
int optname,
1312 char __user *optval,
int __user *optlen)
1314 struct sock *sk = sock->
sk;
1323 val = ipx_sk(sk)->type;
1329 len =
min_t(
unsigned int, len,
sizeof(
int));
1344 static struct proto ipx_proto = {
1347 .obj_size =
sizeof(
struct ipx_sock),
1376 sock->
ops = &ipx_dgram_ops;
1382 static int ipx_release(
struct socket *sock)
1384 struct sock *sk = sock->
sk;
1396 ipx_destroy_socket(sk);
1407 unsigned short socketNum = intrfc->
if_sknum;
1414 while (__ipxitf_find_socket(intrfc,
htons(socketNum)))
1423 return htons(socketNum);
1426 static int __ipx_bind(
struct socket *sock,
1429 struct sock *sk = sock->
sk;
1430 struct ipx_sock *ipxs = ipx_sk(sk);
1444 addr->
sipx_port = ipx_first_free_socketnum(intrfc);
1458 #ifdef CONFIG_IPX_INTERN
1459 if (intrfc == ipx_internal_net) {
1474 if (ipxitf_find_internal_socket(intrfc, ipxs->node,
1477 "IPX: bind failed because port %X in use.\n",
1490 if (ipxitf_find_socket(intrfc, addr->
sipx_port)) {
1492 "IPX: bind failed because port %X in use.\n",
1504 if (ipxitf_find_socket(intrfc, addr->
sipx_port)) {
1505 SOCK_DEBUG(sk,
"IPX: bind failed because port %X in use.\n",
1512 ipxitf_insert_socket(intrfc, sk);
1522 static int ipx_bind(
struct socket *sock,
struct sockaddr *uaddr,
int addr_len)
1524 struct sock *sk = sock->
sk;
1528 rc = __ipx_bind(sock, uaddr, addr_len);
1534 static int ipx_connect(
struct socket *sock,
struct sockaddr *uaddr,
1535 int addr_len,
int flags)
1537 struct sock *sk = sock->
sk;
1538 struct ipx_sock *ipxs = ipx_sk(sk);
1547 if (addr_len !=
sizeof(*addr))
1555 uaddr.sipx_port = 0;
1556 uaddr.sipx_network = 0;
1558 #ifdef CONFIG_IPX_INTERN
1566 rc = __ipx_bind(sock, (
struct sockaddr *)&uaddr,
1598 static int ipx_getname(
struct socket *sock,
struct sockaddr *uaddr,
1599 int *uaddr_len,
int peer)
1603 struct sock *sk = sock->
sk;
1604 struct ipx_sock *ipxs = ipx_sk(sk);
1622 #ifdef CONFIG_IPX_INTERN
1640 memcpy(uaddr, &sipx,
sizeof(sipx));
1656 if (!net_eq(dev_net(dev), &
init_net))
1663 if ((skb = skb_share_check(skb,
GFP_ATOMIC)) == NULL)
1666 if (!pskb_may_pull(skb,
sizeof(
struct ipxhdr)))
1669 ipx_pktsize =
ntohs(ipx_hdr(skb)->ipx_pktsize);
1672 if (ipx_pktsize <
sizeof(
struct ipxhdr) ||
1673 !pskb_may_pull(skb, ipx_pktsize))
1678 ipx->ipx_checksum !=
ipx_cksum(ipx, ipx_pktsize))
1682 IPX_SKB_CB(skb)->ipx_dest_net = ipx->ipx_dest.net;
1683 IPX_SKB_CB(skb)->ipx_source_net = ipx->ipx_source.net;
1686 intrfc = ipxitf_find_using_phys(dev, pt->
type);
1688 if (ipxcfg_auto_create_interfaces &&
1690 intrfc = ipxitf_auto_create(dev, pt->
type);
1692 ipxitf_hold(intrfc);
1700 rc = ipxitf_rcv(intrfc, skb);
1712 struct sock *sk = sock->
sk;
1713 struct ipx_sock *ipxs = ipx_sk(sk);
1727 if (len >= 65535 -
sizeof(
struct ipxhdr))
1734 uaddr.sipx_port = 0;
1735 uaddr.sipx_network = 0;
1736 #ifdef CONFIG_IPX_INTERN
1743 rc = __ipx_bind(sock, (
struct sockaddr *)&uaddr,
1758 usipx = &local_sipx;
1776 static int ipx_recvmsg(
struct kiocb *iocb,
struct socket *sock,
1779 struct sock *sk = sock->
sk;
1780 struct ipx_sock *ipxs = ipx_sk(sk);
1782 struct ipxhdr *ipx = NULL;
1791 uaddr.sipx_port = 0;
1792 uaddr.sipx_network = 0;
1794 #ifdef CONFIG_IPX_INTERN
1801 rc = __ipx_bind(sock, (
struct sockaddr *)&uaddr,
1817 copied =
ntohs(ipx->ipx_pktsize) -
sizeof(
struct ipxhdr);
1818 if (copied > size) {
1850 static int ipx_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1854 struct sock *sk = sock->
sk;
1860 amount = sk->
sk_sndbuf - sk_wmem_alloc_get(sk);
1863 rc =
put_user(amount, (
int __user *)argp);
1870 amount = skb->
len -
sizeof(
struct ipxhdr);
1871 rc =
put_user(amount, (
int __user *)argp);
1887 rc = ipxitf_ioctl(cmd, argp);
1890 rc = ipxcfg_get_config_data(argp);
1900 rc =
get_user(ipx_sk(sk)->ipx_ncp_conn,
1901 (
const unsigned short __user *)argp);
1924 #ifdef CONFIG_COMPAT
1925 static int ipx_compat_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
1938 return ipx_ioctl(sock, cmd, arg);
1952 .create = ipx_create,
1956 static const struct proto_ops ipx_dgram_ops = {
1959 .release = ipx_release,
1961 .connect = ipx_connect,
1964 .getname = ipx_getname,
1967 #ifdef CONFIG_COMPAT
1968 .compat_ioctl = ipx_compat_ioctl,
1972 .setsockopt = ipx_setsockopt,
1973 .getsockopt = ipx_getsockopt,
1974 .sendmsg = ipx_sendmsg,
1975 .recvmsg = ipx_recvmsg,
1985 static struct packet_type ipx_dix_packet_type __read_mostly = {
1991 .notifier_call = ipxitf_device_event,
1997 static const unsigned char ipx_8022_type = 0xE0;
1998 static const unsigned char ipx_snap_id[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
2000 KERN_CRIT "IPX: Unable to register with Ethernet II\n";
2001 static const char ipx_8023_err_msg[] __initconst =
2002 KERN_CRIT "IPX: Unable to register with 802.3\n";
2003 static const char ipx_llc_err_msg[] __initconst =
2004 KERN_CRIT "IPX: Unable to register with 802.2\n";
2005 static const char ipx_snap_err_msg[] __initconst =
2006 KERN_CRIT "IPX: Unable to register with SNAP\n";
2008 static int __init ipx_init(
void)
2027 printk(ipx_8023_err_msg);
2030 if (!p8022_datalink)
2034 if (!pSNAP_datalink)
2035 printk(ipx_snap_err_msg);
2044 static void __exit ipx_proto_finito(
void)
2053 if (pSNAP_datalink) {
2055 pSNAP_datalink =
NULL;
2058 if (p8022_datalink) {
2060 p8022_datalink =
NULL;
2064 if (p8023_datalink) {
2066 p8023_datalink =
NULL;
2070 if (pEII_datalink) {
2072 pEII_datalink =
NULL;