af_ipx.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. /*
  2. * Implements an IPX socket layer.
  3. *
  4. * This code is derived from work by
  5. * Ross Biro : Writing the original IP stack
  6. * Fred Van Kempen : Tidying up the TCP/IP
  7. *
  8. * Many thanks go to Keith Baker, Institute For Industrial Information
  9. * Technology Ltd, Swansea University for allowing me to work on this
  10. * in my own time even though it was in some ways related to commercial
  11. * work I am currently employed to do there.
  12. *
  13. * All the material in this file is subject to the Gnu license version 2.
  14. * Neither Alan Cox nor the Swansea University Computer Society admit
  15. * liability nor provide warranty for any of this software. This material
  16. * is provided as is and at no charge.
  17. *
  18. * Portions Copyright (c) 2000-2003 Conectiva, Inc. <acme@conectiva.com.br>
  19. * Neither Arnaldo Carvalho de Melo nor Conectiva, Inc. admit liability nor
  20. * provide warranty for any of this software. This material is provided
  21. * "AS-IS" and at no charge.
  22. *
  23. * Portions Copyright (c) 1995 Caldera, Inc. <greg@caldera.com>
  24. * Neither Greg Page nor Caldera, Inc. admit liability nor provide
  25. * warranty for any of this software. This material is provided
  26. * "AS-IS" and at no charge.
  27. *
  28. * See net/ipx/ChangeLog.
  29. */
  30. #include <linux/capability.h>
  31. #include <linux/errno.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/init.h>
  35. #include <linux/ipx.h>
  36. #include <linux/kernel.h>
  37. #include <linux/list.h>
  38. #include <linux/module.h>
  39. #include <linux/net.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/uio.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/socket.h>
  44. #include <linux/sockios.h>
  45. #include <linux/string.h>
  46. #include <linux/types.h>
  47. #include <linux/termios.h>
  48. #include <net/ipx.h>
  49. #include <net/p8022.h>
  50. #include <net/psnap.h>
  51. #include <net/sock.h>
  52. #include <net/tcp_states.h>
  53. #include <asm/uaccess.h>
  54. #ifdef CONFIG_SYSCTL
  55. extern void ipx_register_sysctl(void);
  56. extern void ipx_unregister_sysctl(void);
  57. #else
  58. #define ipx_register_sysctl()
  59. #define ipx_unregister_sysctl()
  60. #endif
  61. /* Configuration Variables */
  62. static unsigned char ipxcfg_max_hops = 16;
  63. static char ipxcfg_auto_select_primary;
  64. static char ipxcfg_auto_create_interfaces;
  65. int sysctl_ipx_pprop_broadcasting = 1;
  66. /* Global Variables */
  67. static struct datalink_proto *p8022_datalink;
  68. static struct datalink_proto *pEII_datalink;
  69. static struct datalink_proto *p8023_datalink;
  70. static struct datalink_proto *pSNAP_datalink;
  71. static const struct proto_ops ipx_dgram_ops;
  72. LIST_HEAD(ipx_interfaces);
  73. DEFINE_SPINLOCK(ipx_interfaces_lock);
  74. struct ipx_interface *ipx_primary_net;
  75. struct ipx_interface *ipx_internal_net;
  76. extern int ipxrtr_add_route(__be32 network, struct ipx_interface *intrfc,
  77. unsigned char *node);
  78. extern void ipxrtr_del_routes(struct ipx_interface *intrfc);
  79. extern int ipxrtr_route_packet(struct sock *sk, struct sockaddr_ipx *usipx,
  80. struct iovec *iov, size_t len, int noblock);
  81. extern int ipxrtr_route_skb(struct sk_buff *skb);
  82. extern struct ipx_route *ipxrtr_lookup(__be32 net);
  83. extern int ipxrtr_ioctl(unsigned int cmd, void __user *arg);
  84. #undef IPX_REFCNT_DEBUG
  85. #ifdef IPX_REFCNT_DEBUG
  86. atomic_t ipx_sock_nr;
  87. #endif
  88. struct ipx_interface *ipx_interfaces_head(void)
  89. {
  90. struct ipx_interface *rc = NULL;
  91. if (!list_empty(&ipx_interfaces))
  92. rc = list_entry(ipx_interfaces.next,
  93. struct ipx_interface, node);
  94. return rc;
  95. }
  96. static void ipxcfg_set_auto_select(char val)
  97. {
  98. ipxcfg_auto_select_primary = val;
  99. if (val && !ipx_primary_net)
  100. ipx_primary_net = ipx_interfaces_head();
  101. }
  102. static int ipxcfg_get_config_data(struct ipx_config_data __user *arg)
  103. {
  104. struct ipx_config_data vals;
  105. vals.ipxcfg_auto_create_interfaces = ipxcfg_auto_create_interfaces;
  106. vals.ipxcfg_auto_select_primary = ipxcfg_auto_select_primary;
  107. return copy_to_user(arg, &vals, sizeof(vals)) ? -EFAULT : 0;
  108. }
  109. /*
  110. * Note: Sockets may not be removed _during_ an interrupt or inet_bh
  111. * handler using this technique. They can be added although we do not
  112. * use this facility.
  113. */
  114. static void ipx_remove_socket(struct sock *sk)
  115. {
  116. /* Determine interface with which socket is associated */
  117. struct ipx_interface *intrfc = ipx_sk(sk)->intrfc;
  118. if (!intrfc)
  119. goto out;
  120. ipxitf_hold(intrfc);
  121. spin_lock_bh(&intrfc->if_sklist_lock);
  122. sk_del_node_init(sk);
  123. spin_unlock_bh(&intrfc->if_sklist_lock);
  124. ipxitf_put(intrfc);
  125. out:
  126. return;
  127. }
  128. static void ipx_destroy_socket(struct sock *sk)
  129. {
  130. ipx_remove_socket(sk);
  131. skb_queue_purge(&sk->sk_receive_queue);
  132. #ifdef IPX_REFCNT_DEBUG
  133. atomic_dec(&ipx_sock_nr);
  134. printk(KERN_DEBUG "IPX socket %p released, %d are still alive\n", sk,
  135. atomic_read(&ipx_sock_nr));
  136. if (atomic_read(&sk->sk_refcnt) != 1)
  137. printk(KERN_DEBUG "Destruction sock ipx %p delayed, cnt=%d\n",
  138. sk, atomic_read(&sk->sk_refcnt));
  139. #endif
  140. sock_put(sk);
  141. }
  142. /*
  143. * The following code is used to support IPX Interfaces (IPXITF). An
  144. * IPX interface is defined by a physical device and a frame type.
  145. */
  146. /* ipxitf_clear_primary_net has to be called with ipx_interfaces_lock held */
  147. static void ipxitf_clear_primary_net(void)
  148. {
  149. ipx_primary_net = NULL;
  150. if (ipxcfg_auto_select_primary)
  151. ipx_primary_net = ipx_interfaces_head();
  152. }
  153. static struct ipx_interface *__ipxitf_find_using_phys(struct net_device *dev,
  154. __be16 datalink)
  155. {
  156. struct ipx_interface *i;
  157. list_for_each_entry(i, &ipx_interfaces, node)
  158. if (i->if_dev == dev && i->if_dlink_type == datalink)
  159. goto out;
  160. i = NULL;
  161. out:
  162. return i;
  163. }
  164. static struct ipx_interface *ipxitf_find_using_phys(struct net_device *dev,
  165. __be16 datalink)
  166. {
  167. struct ipx_interface *i;
  168. spin_lock_bh(&ipx_interfaces_lock);
  169. i = __ipxitf_find_using_phys(dev, datalink);
  170. if (i)
  171. ipxitf_hold(i);
  172. spin_unlock_bh(&ipx_interfaces_lock);
  173. return i;
  174. }
  175. struct ipx_interface *ipxitf_find_using_net(__be32 net)
  176. {
  177. struct ipx_interface *i;
  178. spin_lock_bh(&ipx_interfaces_lock);
  179. if (net) {
  180. list_for_each_entry(i, &ipx_interfaces, node)
  181. if (i->if_netnum == net)
  182. goto hold;
  183. i = NULL;
  184. goto unlock;
  185. }
  186. i = ipx_primary_net;
  187. if (i)
  188. hold:
  189. ipxitf_hold(i);
  190. unlock:
  191. spin_unlock_bh(&ipx_interfaces_lock);
  192. return i;
  193. }
  194. /* Sockets are bound to a particular IPX interface. */
  195. static void ipxitf_insert_socket(struct ipx_interface *intrfc, struct sock *sk)
  196. {
  197. ipxitf_hold(intrfc);
  198. spin_lock_bh(&intrfc->if_sklist_lock);
  199. ipx_sk(sk)->intrfc = intrfc;
  200. sk_add_node(sk, &intrfc->if_sklist);
  201. spin_unlock_bh(&intrfc->if_sklist_lock);
  202. ipxitf_put(intrfc);
  203. }
  204. /* caller must hold intrfc->if_sklist_lock */
  205. static struct sock *__ipxitf_find_socket(struct ipx_interface *intrfc,
  206. __be16 port)
  207. {
  208. struct sock *s;
  209. struct hlist_node *node;
  210. sk_for_each(s, node, &intrfc->if_sklist)
  211. if (ipx_sk(s)->port == port)
  212. goto found;
  213. s = NULL;
  214. found:
  215. return s;
  216. }
  217. /* caller must hold a reference to intrfc */
  218. static struct sock *ipxitf_find_socket(struct ipx_interface *intrfc,
  219. __be16 port)
  220. {
  221. struct sock *s;
  222. spin_lock_bh(&intrfc->if_sklist_lock);
  223. s = __ipxitf_find_socket(intrfc, port);
  224. if (s)
  225. sock_hold(s);
  226. spin_unlock_bh(&intrfc->if_sklist_lock);
  227. return s;
  228. }
  229. #ifdef CONFIG_IPX_INTERN
  230. static struct sock *ipxitf_find_internal_socket(struct ipx_interface *intrfc,
  231. unsigned char *ipx_node,
  232. __be16 port)
  233. {
  234. struct sock *s;
  235. struct hlist_node *node;
  236. ipxitf_hold(intrfc);
  237. spin_lock_bh(&intrfc->if_sklist_lock);
  238. sk_for_each(s, node, &intrfc->if_sklist) {
  239. struct ipx_sock *ipxs = ipx_sk(s);
  240. if (ipxs->port == port &&
  241. !memcmp(ipx_node, ipxs->node, IPX_NODE_LEN))
  242. goto found;
  243. }
  244. s = NULL;
  245. found:
  246. spin_unlock_bh(&intrfc->if_sklist_lock);
  247. ipxitf_put(intrfc);
  248. return s;
  249. }
  250. #endif
  251. static void __ipxitf_down(struct ipx_interface *intrfc)
  252. {
  253. struct sock *s;
  254. struct hlist_node *node, *t;
  255. /* Delete all routes associated with this interface */
  256. ipxrtr_del_routes(intrfc);
  257. spin_lock_bh(&intrfc->if_sklist_lock);
  258. /* error sockets */
  259. sk_for_each_safe(s, node, t, &intrfc->if_sklist) {
  260. struct ipx_sock *ipxs = ipx_sk(s);
  261. s->sk_err = ENOLINK;
  262. s->sk_error_report(s);
  263. ipxs->intrfc = NULL;
  264. ipxs->port = 0;
  265. sock_set_flag(s, SOCK_ZAPPED); /* Indicates it is no longer bound */
  266. sk_del_node_init(s);
  267. }
  268. INIT_HLIST_HEAD(&intrfc->if_sklist);
  269. spin_unlock_bh(&intrfc->if_sklist_lock);
  270. /* remove this interface from list */
  271. list_del(&intrfc->node);
  272. /* remove this interface from *special* networks */
  273. if (intrfc == ipx_primary_net)
  274. ipxitf_clear_primary_net();
  275. if (intrfc == ipx_internal_net)
  276. ipx_internal_net = NULL;
  277. if (intrfc->if_dev)
  278. dev_put(intrfc->if_dev);
  279. kfree(intrfc);
  280. }
  281. void ipxitf_down(struct ipx_interface *intrfc)
  282. {
  283. spin_lock_bh(&ipx_interfaces_lock);
  284. __ipxitf_down(intrfc);
  285. spin_unlock_bh(&ipx_interfaces_lock);
  286. }
  287. static __inline__ void __ipxitf_put(struct ipx_interface *intrfc)
  288. {
  289. if (atomic_dec_and_test(&intrfc->refcnt))
  290. __ipxitf_down(intrfc);
  291. }
  292. static int ipxitf_device_event(struct notifier_block *notifier,
  293. unsigned long event, void *ptr)
  294. {
  295. struct net_device *dev = ptr;
  296. struct ipx_interface *i, *tmp;
  297. if (dev->nd_net != &init_net)
  298. return NOTIFY_DONE;
  299. if (event != NETDEV_DOWN && event != NETDEV_UP)
  300. goto out;
  301. spin_lock_bh(&ipx_interfaces_lock);
  302. list_for_each_entry_safe(i, tmp, &ipx_interfaces, node)
  303. if (i->if_dev == dev) {
  304. if (event == NETDEV_UP)
  305. ipxitf_hold(i);
  306. else
  307. __ipxitf_put(i);
  308. }
  309. spin_unlock_bh(&ipx_interfaces_lock);
  310. out:
  311. return NOTIFY_DONE;
  312. }
  313. static __exit void ipxitf_cleanup(void)
  314. {
  315. struct ipx_interface *i, *tmp;
  316. spin_lock_bh(&ipx_interfaces_lock);
  317. list_for_each_entry_safe(i, tmp, &ipx_interfaces, node)
  318. __ipxitf_put(i);
  319. spin_unlock_bh(&ipx_interfaces_lock);
  320. }
  321. static void ipxitf_def_skb_handler(struct sock *sock, struct sk_buff *skb)
  322. {
  323. if (sock_queue_rcv_skb(sock, skb) < 0)
  324. kfree_skb(skb);
  325. }
  326. /*
  327. * On input skb->sk is NULL. Nobody is charged for the memory.
  328. */
  329. /* caller must hold a reference to intrfc */
  330. #ifdef CONFIG_IPX_INTERN
  331. static int ipxitf_demux_socket(struct ipx_interface *intrfc,
  332. struct sk_buff *skb, int copy)
  333. {
  334. struct ipxhdr *ipx = ipx_hdr(skb);
  335. int is_broadcast = !memcmp(ipx->ipx_dest.node, ipx_broadcast_node,
  336. IPX_NODE_LEN);
  337. struct sock *s;
  338. struct hlist_node *node;
  339. int rc;
  340. spin_lock_bh(&intrfc->if_sklist_lock);
  341. sk_for_each(s, node, &intrfc->if_sklist) {
  342. struct ipx_sock *ipxs = ipx_sk(s);
  343. if (ipxs->port == ipx->ipx_dest.sock &&
  344. (is_broadcast || !memcmp(ipx->ipx_dest.node,
  345. ipxs->node, IPX_NODE_LEN))) {
  346. /* We found a socket to which to send */
  347. struct sk_buff *skb1;
  348. if (copy) {
  349. skb1 = skb_clone(skb, GFP_ATOMIC);
  350. rc = -ENOMEM;
  351. if (!skb1)
  352. goto out;
  353. } else {
  354. skb1 = skb;
  355. copy = 1; /* skb may only be used once */
  356. }
  357. ipxitf_def_skb_handler(s, skb1);
  358. /* On an external interface, one socket can listen */
  359. if (intrfc != ipx_internal_net)
  360. break;
  361. }
  362. }
  363. /* skb was solely for us, and we did not make a copy, so free it. */
  364. if (!copy)
  365. kfree_skb(skb);
  366. rc = 0;
  367. out:
  368. spin_unlock_bh(&intrfc->if_sklist_lock);
  369. return rc;
  370. }
  371. #else
  372. static struct sock *ncp_connection_hack(struct ipx_interface *intrfc,
  373. struct ipxhdr *ipx)
  374. {
  375. /* The packet's target is a NCP connection handler. We want to hand it
  376. * to the correct socket directly within the kernel, so that the
  377. * mars_nwe packet distribution process does not have to do it. Here we
  378. * only care about NCP and BURST packets.
  379. *
  380. * You might call this a hack, but believe me, you do not want a
  381. * complete NCP layer in the kernel, and this is VERY fast as well. */
  382. struct sock *sk = NULL;
  383. int connection = 0;
  384. u8 *ncphdr = (u8 *)(ipx + 1);
  385. if (*ncphdr == 0x22 && *(ncphdr + 1) == 0x22) /* NCP request */
  386. connection = (((int) *(ncphdr + 5)) << 8) | (int) *(ncphdr + 3);
  387. else if (*ncphdr == 0x77 && *(ncphdr + 1) == 0x77) /* BURST packet */
  388. connection = (((int) *(ncphdr + 9)) << 8) | (int) *(ncphdr + 8);
  389. if (connection) {
  390. struct hlist_node *node;
  391. /* Now we have to look for a special NCP connection handling
  392. * socket. Only these sockets have ipx_ncp_conn != 0, set by
  393. * SIOCIPXNCPCONN. */
  394. spin_lock_bh(&intrfc->if_sklist_lock);
  395. sk_for_each(sk, node, &intrfc->if_sklist)
  396. if (ipx_sk(sk)->ipx_ncp_conn == connection) {
  397. sock_hold(sk);
  398. goto found;
  399. }
  400. sk = NULL;
  401. found:
  402. spin_unlock_bh(&intrfc->if_sklist_lock);
  403. }
  404. return sk;
  405. }
  406. static int ipxitf_demux_socket(struct ipx_interface *intrfc,
  407. struct sk_buff *skb, int copy)
  408. {
  409. struct ipxhdr *ipx = ipx_hdr(skb);
  410. struct sock *sock1 = NULL, *sock2 = NULL;
  411. struct sk_buff *skb1 = NULL, *skb2 = NULL;
  412. int rc;
  413. if (intrfc == ipx_primary_net && ntohs(ipx->ipx_dest.sock) == 0x451)
  414. sock1 = ncp_connection_hack(intrfc, ipx);
  415. if (!sock1)
  416. /* No special socket found, forward the packet the normal way */
  417. sock1 = ipxitf_find_socket(intrfc, ipx->ipx_dest.sock);
  418. /*
  419. * We need to check if there is a primary net and if
  420. * this is addressed to one of the *SPECIAL* sockets because
  421. * these need to be propagated to the primary net.
  422. * The *SPECIAL* socket list contains: 0x452(SAP), 0x453(RIP) and
  423. * 0x456(Diagnostic).
  424. */
  425. if (ipx_primary_net && intrfc != ipx_primary_net) {
  426. const int dsock = ntohs(ipx->ipx_dest.sock);
  427. if (dsock == 0x452 || dsock == 0x453 || dsock == 0x456)
  428. /* The appropriate thing to do here is to dup the
  429. * packet and route to the primary net interface via
  430. * ipxitf_send; however, we'll cheat and just demux it
  431. * here. */
  432. sock2 = ipxitf_find_socket(ipx_primary_net,
  433. ipx->ipx_dest.sock);
  434. }
  435. /*
  436. * If there is nothing to do return. The kfree will cancel any charging.
  437. */
  438. rc = 0;
  439. if (!sock1 && !sock2) {
  440. if (!copy)
  441. kfree_skb(skb);
  442. goto out;
  443. }
  444. /*
  445. * This next segment of code is a little awkward, but it sets it up
  446. * so that the appropriate number of copies of the SKB are made and
  447. * that skb1 and skb2 point to it (them) so that it (they) can be
  448. * demuxed to sock1 and/or sock2. If we are unable to make enough
  449. * copies, we do as much as is possible.
  450. */
  451. if (copy)
  452. skb1 = skb_clone(skb, GFP_ATOMIC);
  453. else
  454. skb1 = skb;
  455. rc = -ENOMEM;
  456. if (!skb1)
  457. goto out_put;
  458. /* Do we need 2 SKBs? */
  459. if (sock1 && sock2)
  460. skb2 = skb_clone(skb1, GFP_ATOMIC);
  461. else
  462. skb2 = skb1;
  463. if (sock1)
  464. ipxitf_def_skb_handler(sock1, skb1);
  465. if (!skb2)
  466. goto out_put;
  467. if (sock2)
  468. ipxitf_def_skb_handler(sock2, skb2);
  469. rc = 0;
  470. out_put:
  471. if (sock1)
  472. sock_put(sock1);
  473. if (sock2)
  474. sock_put(sock2);
  475. out:
  476. return rc;
  477. }
  478. #endif /* CONFIG_IPX_INTERN */
  479. static struct sk_buff *ipxitf_adjust_skbuff(struct ipx_interface *intrfc,
  480. struct sk_buff *skb)
  481. {
  482. struct sk_buff *skb2;
  483. int in_offset = (unsigned char *)ipx_hdr(skb) - skb->head;
  484. int out_offset = intrfc->if_ipx_offset;
  485. int len;
  486. /* Hopefully, most cases */
  487. if (in_offset >= out_offset)
  488. return skb;
  489. /* Need new SKB */
  490. len = skb->len + out_offset;
  491. skb2 = alloc_skb(len, GFP_ATOMIC);
  492. if (skb2) {
  493. skb_reserve(skb2, out_offset);
  494. skb_reset_network_header(skb2);
  495. skb_reset_transport_header(skb2);
  496. skb_put(skb2, skb->len);
  497. memcpy(ipx_hdr(skb2), ipx_hdr(skb), skb->len);
  498. memcpy(skb2->cb, skb->cb, sizeof(skb->cb));
  499. }
  500. kfree_skb(skb);
  501. return skb2;
  502. }
  503. /* caller must hold a reference to intrfc and the skb has to be unshared */
  504. int ipxitf_send(struct ipx_interface *intrfc, struct sk_buff *skb, char *node)
  505. {
  506. struct ipxhdr *ipx = ipx_hdr(skb);
  507. struct net_device *dev = intrfc->if_dev;
  508. struct datalink_proto *dl = intrfc->if_dlink;
  509. char dest_node[IPX_NODE_LEN];
  510. int send_to_wire = 1;
  511. int addr_len;
  512. ipx->ipx_tctrl = IPX_SKB_CB(skb)->ipx_tctrl;
  513. ipx->ipx_dest.net = IPX_SKB_CB(skb)->ipx_dest_net;
  514. ipx->ipx_source.net = IPX_SKB_CB(skb)->ipx_source_net;
  515. /* see if we need to include the netnum in the route list */
  516. if (IPX_SKB_CB(skb)->last_hop.index >= 0) {
  517. __be32 *last_hop = (__be32 *)(((u8 *) skb->data) +
  518. sizeof(struct ipxhdr) +
  519. IPX_SKB_CB(skb)->last_hop.index *
  520. sizeof(__be32));
  521. *last_hop = IPX_SKB_CB(skb)->last_hop.netnum;
  522. IPX_SKB_CB(skb)->last_hop.index = -1;
  523. }
  524. /*
  525. * We need to know how many skbuffs it will take to send out this
  526. * packet to avoid unnecessary copies.
  527. */
  528. if (!dl || !dev || dev->flags & IFF_LOOPBACK)
  529. send_to_wire = 0; /* No non looped */
  530. /*
  531. * See if this should be demuxed to sockets on this interface
  532. *
  533. * We want to ensure the original was eaten or that we only use
  534. * up clones.
  535. */
  536. if (ipx->ipx_dest.net == intrfc->if_netnum) {
  537. /*
  538. * To our own node, loop and free the original.
  539. * The internal net will receive on all node address.
  540. */
  541. if (intrfc == ipx_internal_net ||
  542. !memcmp(intrfc->if_node, node, IPX_NODE_LEN)) {
  543. /* Don't charge sender */
  544. skb_orphan(skb);
  545. /* Will charge receiver */
  546. return ipxitf_demux_socket(intrfc, skb, 0);
  547. }
  548. /* Broadcast, loop and possibly keep to send on. */
  549. if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN)) {
  550. if (!send_to_wire)
  551. skb_orphan(skb);
  552. ipxitf_demux_socket(intrfc, skb, send_to_wire);
  553. if (!send_to_wire)
  554. goto out;
  555. }
  556. }
  557. /*
  558. * If the originating net is not equal to our net; this is routed
  559. * We are still charging the sender. Which is right - the driver
  560. * free will handle this fairly.
  561. */
  562. if (ipx->ipx_source.net != intrfc->if_netnum) {
  563. /*
  564. * Unshare the buffer before modifying the count in
  565. * case it's a flood or tcpdump
  566. */
  567. skb = skb_unshare(skb, GFP_ATOMIC);
  568. if (!skb)
  569. goto out;
  570. if (++ipx->ipx_tctrl > ipxcfg_max_hops)
  571. send_to_wire = 0;
  572. }
  573. if (!send_to_wire) {
  574. kfree_skb(skb);
  575. goto out;
  576. }
  577. /* Determine the appropriate hardware address */
  578. addr_len = dev->addr_len;
  579. if (!memcmp(ipx_broadcast_node, node, IPX_NODE_LEN))
  580. memcpy(dest_node, dev->broadcast, addr_len);
  581. else
  582. memcpy(dest_node, &(node[IPX_NODE_LEN-addr_len]), addr_len);
  583. /* Make any compensation for differing physical/data link size */
  584. skb = ipxitf_adjust_skbuff(intrfc, skb);
  585. if (!skb)
  586. goto out;
  587. /* set up data link and physical headers */
  588. skb->dev = dev;
  589. skb->protocol = htons(ETH_P_IPX);
  590. /* Send it out */
  591. dl->request(dl, skb, dest_node);
  592. out:
  593. return 0;
  594. }
  595. static int ipxitf_add_local_route(struct ipx_interface *intrfc)
  596. {
  597. return ipxrtr_add_route(intrfc->if_netnum, intrfc, NULL);
  598. }
  599. static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
  600. struct sk_buff *skb);
  601. static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb);
  602. static int ipxitf_rcv(struct ipx_interface *intrfc, struct sk_buff *skb)
  603. {
  604. struct ipxhdr *ipx = ipx_hdr(skb);
  605. int rc = 0;
  606. ipxitf_hold(intrfc);
  607. /* See if we should update our network number */
  608. if (!intrfc->if_netnum) /* net number of intrfc not known yet */
  609. ipxitf_discover_netnum(intrfc, skb);
  610. IPX_SKB_CB(skb)->last_hop.index = -1;
  611. if (ipx->ipx_type == IPX_TYPE_PPROP) {
  612. rc = ipxitf_pprop(intrfc, skb);
  613. if (rc)
  614. goto out_free_skb;
  615. }
  616. /* local processing follows */
  617. if (!IPX_SKB_CB(skb)->ipx_dest_net)
  618. IPX_SKB_CB(skb)->ipx_dest_net = intrfc->if_netnum;
  619. if (!IPX_SKB_CB(skb)->ipx_source_net)
  620. IPX_SKB_CB(skb)->ipx_source_net = intrfc->if_netnum;
  621. /* it doesn't make sense to route a pprop packet, there's no meaning
  622. * in the ipx_dest_net for such packets */
  623. if (ipx->ipx_type != IPX_TYPE_PPROP &&
  624. intrfc->if_netnum != IPX_SKB_CB(skb)->ipx_dest_net) {
  625. /* We only route point-to-point packets. */
  626. if (skb->pkt_type == PACKET_HOST) {
  627. skb = skb_unshare(skb, GFP_ATOMIC);
  628. if (skb)
  629. rc = ipxrtr_route_skb(skb);
  630. goto out_intrfc;
  631. }
  632. goto out_free_skb;
  633. }
  634. /* see if we should keep it */
  635. if (!memcmp(ipx_broadcast_node, ipx->ipx_dest.node, IPX_NODE_LEN) ||
  636. !memcmp(intrfc->if_node, ipx->ipx_dest.node, IPX_NODE_LEN)) {
  637. rc = ipxitf_demux_socket(intrfc, skb, 0);
  638. goto out_intrfc;
  639. }
  640. /* we couldn't pawn it off so unload it */
  641. out_free_skb:
  642. kfree_skb(skb);
  643. out_intrfc:
  644. ipxitf_put(intrfc);
  645. return rc;
  646. }
  647. static void ipxitf_discover_netnum(struct ipx_interface *intrfc,
  648. struct sk_buff *skb)
  649. {
  650. const struct ipx_cb *cb = IPX_SKB_CB(skb);
  651. /* see if this is an intra packet: source_net == dest_net */
  652. if (cb->ipx_source_net == cb->ipx_dest_net && cb->ipx_source_net) {
  653. struct ipx_interface *i =
  654. ipxitf_find_using_net(cb->ipx_source_net);
  655. /* NB: NetWare servers lie about their hop count so we
  656. * dropped the test based on it. This is the best way
  657. * to determine this is a 0 hop count packet. */
  658. if (!i) {
  659. intrfc->if_netnum = cb->ipx_source_net;
  660. ipxitf_add_local_route(intrfc);
  661. } else {
  662. printk(KERN_WARNING "IPX: Network number collision "
  663. "%lx\n %s %s and %s %s\n",
  664. (unsigned long) ntohl(cb->ipx_source_net),
  665. ipx_device_name(i),
  666. ipx_frame_name(i->if_dlink_type),
  667. ipx_device_name(intrfc),
  668. ipx_frame_name(intrfc->if_dlink_type));
  669. ipxitf_put(i);
  670. }
  671. }
  672. }
  673. /**
  674. * ipxitf_pprop - Process packet propagation IPX packet type 0x14, used for
  675. * NetBIOS broadcasts
  676. * @intrfc: IPX interface receiving this packet
  677. * @skb: Received packet
  678. *
  679. * Checks if packet is valid: if its more than %IPX_MAX_PPROP_HOPS hops or if it
  680. * is smaller than a IPX header + the room for %IPX_MAX_PPROP_HOPS hops we drop
  681. * it, not even processing it locally, if it has exact %IPX_MAX_PPROP_HOPS we
  682. * don't broadcast it, but process it locally. See chapter 5 of Novell's "IPX
  683. * RIP and SAP Router Specification", Part Number 107-000029-001.
  684. *
  685. * If it is valid, check if we have pprop broadcasting enabled by the user,
  686. * if not, just return zero for local processing.
  687. *
  688. * If it is enabled check the packet and don't broadcast it if we have already
  689. * seen this packet.
  690. *
  691. * Broadcast: send it to the interfaces that aren't on the packet visited nets
  692. * array, just after the IPX header.
  693. *
  694. * Returns -EINVAL for invalid packets, so that the calling function drops
  695. * the packet without local processing. 0 if packet is to be locally processed.
  696. */
  697. static int ipxitf_pprop(struct ipx_interface *intrfc, struct sk_buff *skb)
  698. {
  699. struct ipxhdr *ipx = ipx_hdr(skb);
  700. int i, rc = -EINVAL;
  701. struct ipx_interface *ifcs;
  702. char *c;
  703. __be32 *l;
  704. /* Illegal packet - too many hops or too short */
  705. /* We decide to throw it away: no broadcasting, no local processing.
  706. * NetBIOS unaware implementations route them as normal packets -
  707. * tctrl <= 15, any data payload... */
  708. if (IPX_SKB_CB(skb)->ipx_tctrl > IPX_MAX_PPROP_HOPS ||
  709. ntohs(ipx->ipx_pktsize) < sizeof(struct ipxhdr) +
  710. IPX_MAX_PPROP_HOPS * sizeof(u32))
  711. goto out;
  712. /* are we broadcasting this damn thing? */
  713. rc = 0;
  714. if (!sysctl_ipx_pprop_broadcasting)
  715. goto out;
  716. /* We do broadcast packet on the IPX_MAX_PPROP_HOPS hop, but we
  717. * process it locally. All previous hops broadcasted it, and process it
  718. * locally. */
  719. if (IPX_SKB_CB(skb)->ipx_tctrl == IPX_MAX_PPROP_HOPS)
  720. goto out;
  721. c = ((u8 *) ipx) + sizeof(struct ipxhdr);
  722. l = (__be32 *) c;
  723. /* Don't broadcast packet if already seen this net */
  724. for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
  725. if (*l++ == intrfc->if_netnum)
  726. goto out;
  727. /* < IPX_MAX_PPROP_HOPS hops && input interface not in list. Save the
  728. * position where we will insert recvd netnum into list, later on,
  729. * in ipxitf_send */
  730. IPX_SKB_CB(skb)->last_hop.index = i;
  731. IPX_SKB_CB(skb)->last_hop.netnum = intrfc->if_netnum;
  732. /* xmit on all other interfaces... */
  733. spin_lock_bh(&ipx_interfaces_lock);
  734. list_for_each_entry(ifcs, &ipx_interfaces, node) {
  735. /* Except unconfigured interfaces */
  736. if (!ifcs->if_netnum)
  737. continue;
  738. /* That aren't in the list */
  739. if (ifcs == intrfc)
  740. continue;
  741. l = (__be32 *) c;
  742. /* don't consider the last entry in the packet list,
  743. * it is our netnum, and it is not there yet */
  744. for (i = 0; i < IPX_SKB_CB(skb)->ipx_tctrl; i++)
  745. if (ifcs->if_netnum == *l++)
  746. break;
  747. if (i == IPX_SKB_CB(skb)->ipx_tctrl) {
  748. struct sk_buff *s = skb_copy(skb, GFP_ATOMIC);
  749. if (s) {
  750. IPX_SKB_CB(s)->ipx_dest_net = ifcs->if_netnum;
  751. ipxrtr_route_skb(s);
  752. }
  753. }
  754. }
  755. spin_unlock_bh(&ipx_interfaces_lock);
  756. out:
  757. return rc;
  758. }
  759. static void ipxitf_insert(struct ipx_interface *intrfc)
  760. {
  761. spin_lock_bh(&ipx_interfaces_lock);
  762. list_add_tail(&intrfc->node, &ipx_interfaces);
  763. spin_unlock_bh(&ipx_interfaces_lock);
  764. if (ipxcfg_auto_select_primary && !ipx_primary_net)
  765. ipx_primary_net = intrfc;
  766. }
  767. static struct ipx_interface *ipxitf_alloc(struct net_device *dev, __be32 netnum,
  768. __be16 dlink_type,
  769. struct datalink_proto *dlink,
  770. unsigned char internal,
  771. int ipx_offset)
  772. {
  773. struct ipx_interface *intrfc = kmalloc(sizeof(*intrfc), GFP_ATOMIC);
  774. if (intrfc) {
  775. intrfc->if_dev = dev;
  776. intrfc->if_netnum = netnum;
  777. intrfc->if_dlink_type = dlink_type;
  778. intrfc->if_dlink = dlink;
  779. intrfc->if_internal = internal;
  780. intrfc->if_ipx_offset = ipx_offset;
  781. intrfc->if_sknum = IPX_MIN_EPHEMERAL_SOCKET;
  782. INIT_HLIST_HEAD(&intrfc->if_sklist);
  783. atomic_set(&intrfc->refcnt, 1);
  784. spin_lock_init(&intrfc->if_sklist_lock);
  785. }
  786. return intrfc;
  787. }
  788. static int ipxitf_create_internal(struct ipx_interface_definition *idef)
  789. {
  790. struct ipx_interface *intrfc;
  791. int rc = -EEXIST;
  792. /* Only one primary network allowed */
  793. if (ipx_primary_net)
  794. goto out;
  795. /* Must have a valid network number */
  796. rc = -EADDRNOTAVAIL;
  797. if (!idef->ipx_network)
  798. goto out;
  799. intrfc = ipxitf_find_using_net(idef->ipx_network);
  800. rc = -EADDRINUSE;
  801. if (intrfc) {
  802. ipxitf_put(intrfc);
  803. goto out;
  804. }
  805. intrfc = ipxitf_alloc(NULL, idef->ipx_network, 0, NULL, 1, 0);
  806. rc = -EAGAIN;
  807. if (!intrfc)
  808. goto out;
  809. memcpy((char *)&(intrfc->if_node), idef->ipx_node, IPX_NODE_LEN);
  810. ipx_internal_net = ipx_primary_net = intrfc;
  811. ipxitf_hold(intrfc);
  812. ipxitf_insert(intrfc);
  813. rc = ipxitf_add_local_route(intrfc);
  814. ipxitf_put(intrfc);
  815. out:
  816. return rc;
  817. }
  818. static __be16 ipx_map_frame_type(unsigned char type)
  819. {
  820. __be16 rc = 0;
  821. switch (type) {
  822. case IPX_FRAME_ETHERII: rc = htons(ETH_P_IPX); break;
  823. case IPX_FRAME_8022: rc = htons(ETH_P_802_2); break;
  824. case IPX_FRAME_SNAP: rc = htons(ETH_P_SNAP); break;
  825. case IPX_FRAME_8023: rc = htons(ETH_P_802_3); break;
  826. }
  827. return rc;
  828. }
  829. static int ipxitf_create(struct ipx_interface_definition *idef)
  830. {
  831. struct net_device *dev;
  832. __be16 dlink_type = 0;
  833. struct datalink_proto *datalink = NULL;
  834. struct ipx_interface *intrfc;
  835. int rc;
  836. if (idef->ipx_special == IPX_INTERNAL) {
  837. rc = ipxitf_create_internal(idef);
  838. goto out;
  839. }
  840. rc = -EEXIST;
  841. if (idef->ipx_special == IPX_PRIMARY && ipx_primary_net)
  842. goto out;
  843. intrfc = ipxitf_find_using_net(idef->ipx_network);
  844. rc = -EADDRINUSE;
  845. if (idef->ipx_network && intrfc) {
  846. ipxitf_put(intrfc);
  847. goto out;
  848. }
  849. if (intrfc)
  850. ipxitf_put(intrfc);
  851. dev = dev_get_by_name(&init_net, idef->ipx_device);
  852. rc = -ENODEV;
  853. if (!dev)
  854. goto out;
  855. switch (idef->ipx_dlink_type) {
  856. case IPX_FRAME_TR_8022:
  857. printk(KERN_WARNING "IPX frame type 802.2TR is "
  858. "obsolete Use 802.2 instead.\n");
  859. /* fall through */
  860. case IPX_FRAME_8022:
  861. dlink_type = htons(ETH_P_802_2);
  862. datalink = p8022_datalink;
  863. break;
  864. case IPX_FRAME_ETHERII:
  865. if (dev->type != ARPHRD_IEEE802) {
  866. dlink_type = htons(ETH_P_IPX);
  867. datalink = pEII_datalink;
  868. break;
  869. } else
  870. printk(KERN_WARNING "IPX frame type EtherII over "
  871. "token-ring is obsolete. Use SNAP "
  872. "instead.\n");
  873. /* fall through */
  874. case IPX_FRAME_SNAP:
  875. dlink_type = htons(ETH_P_SNAP);
  876. datalink = pSNAP_datalink;
  877. break;
  878. case IPX_FRAME_8023:
  879. dlink_type = htons(ETH_P_802_3);
  880. datalink = p8023_datalink;
  881. break;
  882. case IPX_FRAME_NONE:
  883. default:
  884. rc = -EPROTONOSUPPORT;
  885. goto out_dev;
  886. }
  887. rc = -ENETDOWN;
  888. if (!(dev->flags & IFF_UP))
  889. goto out_dev;
  890. /* Check addresses are suitable */
  891. rc = -EINVAL;
  892. if (dev->addr_len > IPX_NODE_LEN)
  893. goto out_dev;
  894. intrfc = ipxitf_find_using_phys(dev, dlink_type);
  895. if (!intrfc) {
  896. /* Ok now create */
  897. intrfc = ipxitf_alloc(dev, idef->ipx_network, dlink_type,
  898. datalink, 0, dev->hard_header_len +
  899. datalink->header_length);
  900. rc = -EAGAIN;
  901. if (!intrfc)
  902. goto out_dev;
  903. /* Setup primary if necessary */
  904. if (idef->ipx_special == IPX_PRIMARY)
  905. ipx_primary_net = intrfc;
  906. if (!memcmp(idef->ipx_node, "\000\000\000\000\000\000",
  907. IPX_NODE_LEN)) {
  908. memset(intrfc->if_node, 0, IPX_NODE_LEN);
  909. memcpy(intrfc->if_node + IPX_NODE_LEN - dev->addr_len,
  910. dev->dev_addr, dev->addr_len);
  911. } else
  912. memcpy(intrfc->if_node, idef->ipx_node, IPX_NODE_LEN);
  913. ipxitf_hold(intrfc);
  914. ipxitf_insert(intrfc);
  915. }
  916. /* If the network number is known, add a route */
  917. rc = 0;
  918. if (!intrfc->if_netnum)
  919. goto out_intrfc;
  920. rc = ipxitf_add_local_route(intrfc);
  921. out_intrfc:
  922. ipxitf_put(intrfc);
  923. goto out;
  924. out_dev:
  925. dev_put(dev);
  926. out:
  927. return rc;
  928. }
  929. static int ipxitf_delete(struct ipx_interface_definition *idef)
  930. {
  931. struct net_device *dev = NULL;
  932. __be16 dlink_type = 0;
  933. struct ipx_interface *intrfc;
  934. int rc = 0;
  935. spin_lock_bh(&ipx_interfaces_lock);
  936. if (idef->ipx_special == IPX_INTERNAL) {
  937. if (ipx_internal_net) {
  938. __ipxitf_put(ipx_internal_net);
  939. goto out;
  940. }
  941. rc = -ENOENT;
  942. goto out;
  943. }
  944. dlink_type = ipx_map_frame_type(idef->ipx_dlink_type);
  945. rc = -EPROTONOSUPPORT;
  946. if (!dlink_type)
  947. goto out;
  948. dev = __dev_get_by_name(&init_net, idef->ipx_device);
  949. rc = -ENODEV;
  950. if (!dev)
  951. goto out;
  952. intrfc = __ipxitf_find_using_phys(dev, dlink_type);
  953. rc = -EINVAL;
  954. if (!intrfc)
  955. goto out;
  956. __ipxitf_put(intrfc);
  957. rc = 0;
  958. out:
  959. spin_unlock_bh(&ipx_interfaces_lock);
  960. return rc;
  961. }
  962. static struct ipx_interface *ipxitf_auto_create(struct net_device *dev,
  963. __be16 dlink_type)
  964. {
  965. struct ipx_interface *intrfc = NULL;
  966. struct datalink_proto *datalink;
  967. if (!dev)
  968. goto out;
  969. /* Check addresses are suitable */
  970. if (dev->addr_len > IPX_NODE_LEN)
  971. goto out;
  972. switch (ntohs(dlink_type)) {
  973. case ETH_P_IPX: datalink = pEII_datalink; break;
  974. case ETH_P_802_2: datalink = p8022_datalink; break;
  975. case ETH_P_SNAP: datalink = pSNAP_datalink; break;
  976. case ETH_P_802_3: datalink = p8023_datalink; break;
  977. default: goto out;
  978. }
  979. intrfc = ipxitf_alloc(dev, 0, dlink_type, datalink, 0,
  980. dev->hard_header_len + datalink->header_length);
  981. if (intrfc) {
  982. memset(intrfc->if_node, 0, IPX_NODE_LEN);
  983. memcpy((char *)&(intrfc->if_node[IPX_NODE_LEN-dev->addr_len]),
  984. dev->dev_addr, dev->addr_len);
  985. spin_lock_init(&intrfc->if_sklist_lock);
  986. atomic_set(&intrfc->refcnt, 1);
  987. ipxitf_insert(intrfc);
  988. dev_hold(dev);
  989. }
  990. out:
  991. return intrfc;
  992. }
  993. static int ipxitf_ioctl(unsigned int cmd, void __user *arg)
  994. {
  995. int rc = -EINVAL;
  996. struct ifreq ifr;
  997. int val;
  998. switch (cmd) {
  999. case SIOCSIFADDR: {
  1000. struct sockaddr_ipx *sipx;
  1001. struct ipx_interface_definition f;
  1002. rc = -EFAULT;
  1003. if (copy_from_user(&ifr, arg, sizeof(ifr)))
  1004. break;
  1005. sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
  1006. rc = -EINVAL;
  1007. if (sipx->sipx_family != AF_IPX)
  1008. break;
  1009. f.ipx_network = sipx->sipx_network;
  1010. memcpy(f.ipx_device, ifr.ifr_name,
  1011. sizeof(f.ipx_device));
  1012. memcpy(f.ipx_node, sipx->sipx_node, IPX_NODE_LEN);
  1013. f.ipx_dlink_type = sipx->sipx_type;
  1014. f.ipx_special = sipx->sipx_special;
  1015. if (sipx->sipx_action == IPX_DLTITF)
  1016. rc = ipxitf_delete(&f);
  1017. else
  1018. rc = ipxitf_create(&f);
  1019. break;
  1020. }
  1021. case SIOCGIFADDR: {
  1022. struct sockaddr_ipx *sipx;
  1023. struct ipx_interface *ipxif;
  1024. struct net_device *dev;
  1025. rc = -EFAULT;
  1026. if (copy_from_user(&ifr, arg, sizeof(ifr)))
  1027. break;
  1028. sipx = (struct sockaddr_ipx *)&ifr.ifr_addr;
  1029. dev = __dev_get_by_name(&init_net, ifr.ifr_name);
  1030. rc = -ENODEV;
  1031. if (!dev)
  1032. break;
  1033. ipxif = ipxitf_find_using_phys(dev,
  1034. ipx_map_frame_type(sipx->sipx_type));
  1035. rc = -EADDRNOTAVAIL;
  1036. if (!ipxif)
  1037. break;
  1038. sipx->sipx_family = AF_IPX;
  1039. sipx->sipx_network = ipxif->if_netnum;
  1040. memcpy(sipx->sipx_node, ipxif->if_node,
  1041. sizeof(sipx->sipx_node));
  1042. rc = -EFAULT;
  1043. if (copy_to_user(arg, &ifr, sizeof(ifr)))
  1044. break;
  1045. ipxitf_put(ipxif);
  1046. rc = 0;
  1047. break;
  1048. }
  1049. case SIOCAIPXITFCRT:
  1050. rc = -EFAULT;
  1051. if (get_user(val, (unsigned char __user *) arg))
  1052. break;
  1053. rc = 0;
  1054. ipxcfg_auto_create_interfaces = val;
  1055. break;
  1056. case SIOCAIPXPRISLT:
  1057. rc = -EFAULT;
  1058. if (get_user(val, (unsigned char __user *) arg))
  1059. break;
  1060. rc = 0;
  1061. ipxcfg_set_auto_select(val);
  1062. break;
  1063. }
  1064. return rc;
  1065. }
  1066. /*
  1067. * Checksum routine for IPX
  1068. */
  1069. /* Note: We assume ipx_tctrl==0 and htons(length)==ipx_pktsize */
  1070. /* This functions should *not* mess with packet contents */
  1071. __be16 ipx_cksum(struct ipxhdr *packet, int length)
  1072. {
  1073. /*
  1074. * NOTE: sum is a net byte order quantity, which optimizes the
  1075. * loop. This only works on big and little endian machines. (I
  1076. * don't know of a machine that isn't.)
  1077. */
  1078. /* handle the first 3 words separately; checksum should be skipped
  1079. * and ipx_tctrl masked out */
  1080. __u16 *p = (__u16 *)packet;
  1081. __u32 sum = p[1] + (p[2] & (__force u16)htons(0x00ff));
  1082. __u32 i = (length >> 1) - 3; /* Number of remaining complete words */
  1083. /* Loop through them */
  1084. p += 3;
  1085. while (i--)
  1086. sum += *p++;
  1087. /* Add on the last part word if it exists */
  1088. if (packet->ipx_pktsize & htons(1))
  1089. sum += (__force u16)htons(0xff00) & *p;
  1090. /* Do final fixup */
  1091. sum = (sum & 0xffff) + (sum >> 16);
  1092. /* It's a pity there's no concept of carry in C */
  1093. if (sum >= 0x10000)
  1094. sum++;
  1095. /*
  1096. * Leave 0 alone; we don't want 0xffff here. Note that we can't get
  1097. * here with 0x10000, so this check is the same as ((__u16)sum)
  1098. */
  1099. if (sum)
  1100. sum = ~sum;
  1101. return (__force __be16)sum;
  1102. }
  1103. const char *ipx_frame_name(__be16 frame)
  1104. {
  1105. char* rc = "None";
  1106. switch (ntohs(frame)) {
  1107. case ETH_P_IPX: rc = "EtherII"; break;
  1108. case ETH_P_802_2: rc = "802.2"; break;
  1109. case ETH_P_SNAP: rc = "SNAP"; break;
  1110. case ETH_P_802_3: rc = "802.3"; break;
  1111. case ETH_P_TR_802_2: rc = "802.2TR"; break;
  1112. }
  1113. return rc;
  1114. }
  1115. const char *ipx_device_name(struct ipx_interface *intrfc)
  1116. {
  1117. return intrfc->if_internal ? "Internal" :
  1118. intrfc->if_dev ? intrfc->if_dev->name : "Unknown";
  1119. }
  1120. /* Handling for system calls applied via the various interfaces to an IPX
  1121. * socket object. */
  1122. static int ipx_setsockopt(struct socket *sock, int level, int optname,
  1123. char __user *optval, int optlen)
  1124. {
  1125. struct sock *sk = sock->sk;
  1126. int opt;
  1127. int rc = -EINVAL;
  1128. if (optlen != sizeof(int))
  1129. goto out;
  1130. rc = -EFAULT;
  1131. if (get_user(opt, (unsigned int __user *)optval))
  1132. goto out;
  1133. rc = -ENOPROTOOPT;
  1134. if (!(level == SOL_IPX && optname == IPX_TYPE))
  1135. goto out;
  1136. ipx_sk(sk)->type = opt;
  1137. rc = 0;
  1138. out:
  1139. return rc;
  1140. }
  1141. static int ipx_getsockopt(struct socket *sock, int level, int optname,
  1142. char __user *optval, int __user *optlen)
  1143. {
  1144. struct sock *sk = sock->sk;
  1145. int val = 0;
  1146. int len;
  1147. int rc = -ENOPROTOOPT;
  1148. if (!(level == SOL_IPX && optname == IPX_TYPE))
  1149. goto out;
  1150. val = ipx_sk(sk)->type;
  1151. rc = -EFAULT;
  1152. if (get_user(len, optlen))
  1153. goto out;
  1154. len = min_t(unsigned int, len, sizeof(int));
  1155. rc = -EINVAL;
  1156. if(len < 0)
  1157. goto out;
  1158. rc = -EFAULT;
  1159. if (put_user(len, optlen) || copy_to_user(optval, &val, len))
  1160. goto out;
  1161. rc = 0;
  1162. out:
  1163. return rc;
  1164. }
  1165. static struct proto ipx_proto = {
  1166. .name = "IPX",
  1167. .owner = THIS_MODULE,
  1168. .obj_size = sizeof(struct ipx_sock),
  1169. };
  1170. static int ipx_create(struct net *net, struct socket *sock, int protocol)
  1171. {
  1172. int rc = -ESOCKTNOSUPPORT;
  1173. struct sock *sk;
  1174. if (net != &init_net)
  1175. return -EAFNOSUPPORT;
  1176. /*
  1177. * SPX support is not anymore in the kernel sources. If you want to
  1178. * ressurrect it, completing it and making it understand shared skbs,
  1179. * be fully multithreaded, etc, grab the sources in an early 2.5 kernel
  1180. * tree.
  1181. */
  1182. if (sock->type != SOCK_DGRAM)
  1183. goto out;
  1184. rc = -ENOMEM;
  1185. sk = sk_alloc(net, PF_IPX, GFP_KERNEL, &ipx_proto);
  1186. if (!sk)
  1187. goto out;
  1188. #ifdef IPX_REFCNT_DEBUG
  1189. atomic_inc(&ipx_sock_nr);
  1190. printk(KERN_DEBUG "IPX socket %p created, now we have %d alive\n", sk,
  1191. atomic_read(&ipx_sock_nr));
  1192. #endif
  1193. sock_init_data(sock, sk);
  1194. sk->sk_no_check = 1; /* Checksum off by default */
  1195. sock->ops = &ipx_dgram_ops;
  1196. rc = 0;
  1197. out:
  1198. return rc;
  1199. }
  1200. static int ipx_release(struct socket *sock)
  1201. {
  1202. struct sock *sk = sock->sk;
  1203. if (!sk)
  1204. goto out;
  1205. if (!sock_flag(sk, SOCK_DEAD))
  1206. sk->sk_state_change(sk);
  1207. sock_set_flag(sk, SOCK_DEAD);
  1208. sock->sk = NULL;
  1209. ipx_destroy_socket(sk);
  1210. out:
  1211. return 0;
  1212. }
  1213. /* caller must hold a reference to intrfc */
  1214. static __be16 ipx_first_free_socketnum(struct ipx_interface *intrfc)
  1215. {
  1216. unsigned short socketNum = intrfc->if_sknum;
  1217. spin_lock_bh(&intrfc->if_sklist_lock);
  1218. if (socketNum < IPX_MIN_EPHEMERAL_SOCKET)
  1219. socketNum = IPX_MIN_EPHEMERAL_SOCKET;
  1220. while (__ipxitf_find_socket(intrfc, htons(socketNum)))
  1221. if (socketNum > IPX_MAX_EPHEMERAL_SOCKET)
  1222. socketNum = IPX_MIN_EPHEMERAL_SOCKET;
  1223. else
  1224. socketNum++;
  1225. spin_unlock_bh(&intrfc->if_sklist_lock);
  1226. intrfc->if_sknum = socketNum;
  1227. return htons(socketNum);
  1228. }
  1229. static int ipx_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  1230. {
  1231. struct sock *sk = sock->sk;
  1232. struct ipx_sock *ipxs = ipx_sk(sk);
  1233. struct ipx_interface *intrfc;
  1234. struct sockaddr_ipx *addr = (struct sockaddr_ipx *)uaddr;
  1235. int rc = -EINVAL;
  1236. if (!sock_flag(sk, SOCK_ZAPPED) || addr_len != sizeof(struct sockaddr_ipx))
  1237. goto out;
  1238. intrfc = ipxitf_find_using_net(addr->sipx_network);
  1239. rc = -EADDRNOTAVAIL;
  1240. if (!intrfc)
  1241. goto out;
  1242. if (!addr->sipx_port) {
  1243. addr->sipx_port = ipx_first_free_socketnum(intrfc);
  1244. rc = -EINVAL;
  1245. if (!addr->sipx_port)
  1246. goto out_put;
  1247. }
  1248. /* protect IPX system stuff like routing/sap */
  1249. rc = -EACCES;
  1250. if (ntohs(addr->sipx_port) < IPX_MIN_EPHEMERAL_SOCKET &&
  1251. !capable(CAP_NET_ADMIN))
  1252. goto out_put;
  1253. ipxs->port = addr->sipx_port;
  1254. #ifdef CONFIG_IPX_INTERN
  1255. if (intrfc == ipx_internal_net) {
  1256. /* The source address is to be set explicitly if the
  1257. * socket is to be bound on the internal network. If a
  1258. * node number 0 was specified, the default is used.
  1259. */
  1260. rc = -EINVAL;
  1261. if (!memcmp(addr->sipx_node, ipx_broadcast_node, IPX_NODE_LEN))
  1262. goto out_put;
  1263. if (!memcmp(addr->sipx_node, ipx_this_node, IPX_NODE_LEN))
  1264. memcpy(ipxs->node, intrfc->if_node, IPX_NODE_LEN);
  1265. else
  1266. memcpy(ipxs->node, addr->sipx_node, IPX_NODE_LEN);
  1267. rc = -EADDRINUSE;
  1268. if (ipxitf_find_internal_socket(intrfc, ipxs->node,
  1269. ipxs->port)) {
  1270. SOCK_DEBUG(sk,
  1271. "IPX: bind failed because port %X in use.\n",
  1272. ntohs(addr->sipx_port));
  1273. goto out_put;
  1274. }
  1275. } else {
  1276. /* Source addresses are easy. It must be our
  1277. * network:node pair for an interface routed to IPX
  1278. * with the ipx routing ioctl()
  1279. */
  1280. memcpy(ipxs->node, intrfc->if_node, IPX_NODE_LEN);
  1281. rc = -EADDRINUSE;
  1282. if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
  1283. SOCK_DEBUG(sk,
  1284. "IPX: bind failed because port %X in use.\n",
  1285. ntohs(addr->sipx_port));
  1286. goto out_put;
  1287. }
  1288. }
  1289. #else /* !def CONFIG_IPX_INTERN */
  1290. /* Source addresses are easy. It must be our network:node pair for
  1291. an interface routed to IPX with the ipx routing ioctl() */
  1292. rc = -EADDRINUSE;
  1293. if (ipxitf_find_socket(intrfc, addr->sipx_port)) {
  1294. SOCK_DEBUG(sk, "IPX: bind failed because port %X in use.\n",
  1295. ntohs((int)addr->sipx_port));
  1296. goto out_put;
  1297. }
  1298. #endif /* CONFIG_IPX_INTERN */
  1299. ipxitf_insert_socket(intrfc, sk);
  1300. sock_reset_flag(sk, SOCK_ZAPPED);
  1301. rc = 0;
  1302. out_put:
  1303. ipxitf_put(intrfc);
  1304. out:
  1305. return rc;
  1306. }
  1307. static int ipx_connect(struct socket *sock, struct sockaddr *uaddr,
  1308. int addr_len, int flags)
  1309. {
  1310. struct sock *sk = sock->sk;
  1311. struct ipx_sock *ipxs = ipx_sk(sk);
  1312. struct sockaddr_ipx *addr;
  1313. int rc = -EINVAL;
  1314. struct ipx_route *rt;
  1315. sk->sk_state = TCP_CLOSE;
  1316. sock->state = SS_UNCONNECTED;
  1317. if (addr_len != sizeof(*addr))
  1318. goto out;
  1319. addr = (struct sockaddr_ipx *)uaddr;
  1320. /* put the autobinding in */
  1321. if (!ipxs->port) {
  1322. struct sockaddr_ipx uaddr;
  1323. uaddr.sipx_port = 0;
  1324. uaddr.sipx_network = 0;
  1325. #ifdef CONFIG_IPX_INTERN
  1326. rc = -ENETDOWN;
  1327. if (!ipxs->intrfc)
  1328. goto out; /* Someone zonked the iface */
  1329. memcpy(uaddr.sipx_node, ipxs->intrfc->if_node,
  1330. IPX_NODE_LEN);
  1331. #endif /* CONFIG_IPX_INTERN */
  1332. rc = ipx_bind(sock, (struct sockaddr *)&uaddr,
  1333. sizeof(struct sockaddr_ipx));
  1334. if (rc)
  1335. goto out;
  1336. }
  1337. /* We can either connect to primary network or somewhere
  1338. * we can route to */
  1339. rt = ipxrtr_lookup(addr->sipx_network);
  1340. rc = -ENETUNREACH;
  1341. if (!rt && !(!addr->sipx_network && ipx_primary_net))
  1342. goto out;
  1343. ipxs->dest_addr.net = addr->sipx_network;
  1344. ipxs->dest_addr.sock = addr->sipx_port;
  1345. memcpy(ipxs->dest_addr.node, addr->sipx_node, IPX_NODE_LEN);
  1346. ipxs->type = addr->sipx_type;
  1347. if (sock->type == SOCK_DGRAM) {
  1348. sock->state = SS_CONNECTED;
  1349. sk->sk_state = TCP_ESTABLISHED;
  1350. }
  1351. if (rt)
  1352. ipxrtr_put(rt);
  1353. rc = 0;
  1354. out:
  1355. return rc;
  1356. }
  1357. static int ipx_getname(struct socket *sock, struct sockaddr *uaddr,
  1358. int *uaddr_len, int peer)
  1359. {
  1360. struct ipx_address *addr;
  1361. struct sockaddr_ipx sipx;
  1362. struct sock *sk = sock->sk;
  1363. struct ipx_sock *ipxs = ipx_sk(sk);
  1364. int rc;
  1365. *uaddr_len = sizeof(struct sockaddr_ipx);
  1366. if (peer) {
  1367. rc = -ENOTCONN;
  1368. if (sk->sk_state != TCP_ESTABLISHED)
  1369. goto out;
  1370. addr = &ipxs->dest_addr;
  1371. sipx.sipx_network = addr->net;
  1372. sipx.sipx_port = addr->sock;
  1373. memcpy(sipx.sipx_node, addr->node, IPX_NODE_LEN);
  1374. } else {
  1375. if (ipxs->intrfc) {
  1376. sipx.sipx_network = ipxs->intrfc->if_netnum;
  1377. #ifdef CONFIG_IPX_INTERN
  1378. memcpy(sipx.sipx_node, ipxs->node, IPX_NODE_LEN);
  1379. #else
  1380. memcpy(sipx.sipx_node, ipxs->intrfc->if_node,
  1381. IPX_NODE_LEN);
  1382. #endif /* CONFIG_IPX_INTERN */
  1383. } else {
  1384. sipx.sipx_network = 0;
  1385. memset(sipx.sipx_node, '\0', IPX_NODE_LEN);
  1386. }
  1387. sipx.sipx_port = ipxs->port;
  1388. }
  1389. sipx.sipx_family = AF_IPX;
  1390. sipx.sipx_type = ipxs->type;
  1391. sipx.sipx_zero = 0;
  1392. memcpy(uaddr, &sipx, sizeof(sipx));
  1393. rc = 0;
  1394. out:
  1395. return rc;
  1396. }
  1397. static int ipx_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
  1398. {
  1399. /* NULL here for pt means the packet was looped back */
  1400. struct ipx_interface *intrfc;
  1401. struct ipxhdr *ipx;
  1402. u16 ipx_pktsize;
  1403. int rc = 0;
  1404. if (dev->nd_net != &init_net)
  1405. goto drop;
  1406. /* Not ours */
  1407. if (skb->pkt_type == PACKET_OTHERHOST)
  1408. goto drop;
  1409. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  1410. goto out;
  1411. if (!pskb_may_pull(skb, sizeof(struct ipxhdr)))
  1412. goto drop;
  1413. ipx_pktsize = ntohs(ipx_hdr(skb)->ipx_pktsize);
  1414. /* Too small or invalid header? */
  1415. if (ipx_pktsize < sizeof(struct ipxhdr) ||
  1416. !pskb_may_pull(skb, ipx_pktsize))
  1417. goto drop;
  1418. ipx = ipx_hdr(skb);
  1419. if (ipx->ipx_checksum != IPX_NO_CHECKSUM &&
  1420. ipx->ipx_checksum != ipx_cksum(ipx, ipx_pktsize))
  1421. goto drop;
  1422. IPX_SKB_CB(skb)->ipx_tctrl = ipx->ipx_tctrl;
  1423. IPX_SKB_CB(skb)->ipx_dest_net = ipx->ipx_dest.net;
  1424. IPX_SKB_CB(skb)->ipx_source_net = ipx->ipx_source.net;
  1425. /* Determine what local ipx endpoint this is */
  1426. intrfc = ipxitf_find_using_phys(dev, pt->type);
  1427. if (!intrfc) {
  1428. if (ipxcfg_auto_create_interfaces &&
  1429. IPX_SKB_CB(skb)->ipx_dest_net) {
  1430. intrfc = ipxitf_auto_create(dev, pt->type);
  1431. if (intrfc)
  1432. ipxitf_hold(intrfc);
  1433. }
  1434. if (!intrfc) /* Not one of ours */
  1435. /* or invalid packet for auto creation */
  1436. goto drop;
  1437. }
  1438. rc = ipxitf_rcv(intrfc, skb);
  1439. ipxitf_put(intrfc);
  1440. goto out;
  1441. drop:
  1442. kfree_skb(skb);
  1443. out:
  1444. return rc;
  1445. }
  1446. static int ipx_sendmsg(struct kiocb *iocb, struct socket *sock,
  1447. struct msghdr *msg, size_t len)
  1448. {
  1449. struct sock *sk = sock->sk;
  1450. struct ipx_sock *ipxs = ipx_sk(sk);
  1451. struct sockaddr_ipx *usipx = (struct sockaddr_ipx *)msg->msg_name;
  1452. struct sockaddr_ipx local_sipx;
  1453. int rc = -EINVAL;
  1454. int flags = msg->msg_flags;
  1455. /* Socket gets bound below anyway */
  1456. /* if (sk->sk_zapped)
  1457. return -EIO; */ /* Socket not bound */
  1458. if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
  1459. goto out;
  1460. /* Max possible packet size limited by 16 bit pktsize in header */
  1461. if (len >= 65535 - sizeof(struct ipxhdr))
  1462. goto out;
  1463. if (usipx) {
  1464. if (!ipxs->port) {
  1465. struct sockaddr_ipx uaddr;
  1466. uaddr.sipx_port = 0;
  1467. uaddr.sipx_network = 0;
  1468. #ifdef CONFIG_IPX_INTERN
  1469. rc = -ENETDOWN;
  1470. if (!ipxs->intrfc)
  1471. goto out; /* Someone zonked the iface */
  1472. memcpy(uaddr.sipx_node, ipxs->intrfc->if_node,
  1473. IPX_NODE_LEN);
  1474. #endif
  1475. rc = ipx_bind(sock, (struct sockaddr *)&uaddr,
  1476. sizeof(struct sockaddr_ipx));
  1477. if (rc)
  1478. goto out;
  1479. }
  1480. rc = -EINVAL;
  1481. if (msg->msg_namelen < sizeof(*usipx) ||
  1482. usipx->sipx_family != AF_IPX)
  1483. goto out;
  1484. } else {
  1485. rc = -ENOTCONN;
  1486. if (sk->sk_state != TCP_ESTABLISHED)
  1487. goto out;
  1488. usipx = &local_sipx;
  1489. usipx->sipx_family = AF_IPX;
  1490. usipx->sipx_type = ipxs->type;
  1491. usipx->sipx_port = ipxs->dest_addr.sock;
  1492. usipx->sipx_network = ipxs->dest_addr.net;
  1493. memcpy(usipx->sipx_node, ipxs->dest_addr.node, IPX_NODE_LEN);
  1494. }
  1495. rc = ipxrtr_route_packet(sk, usipx, msg->msg_iov, len,
  1496. flags & MSG_DONTWAIT);
  1497. if (rc >= 0)
  1498. rc = len;
  1499. out:
  1500. return rc;
  1501. }
  1502. static int ipx_recvmsg(struct kiocb *iocb, struct socket *sock,
  1503. struct msghdr *msg, size_t size, int flags)
  1504. {
  1505. struct sock *sk = sock->sk;
  1506. struct ipx_sock *ipxs = ipx_sk(sk);
  1507. struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)msg->msg_name;
  1508. struct ipxhdr *ipx = NULL;
  1509. struct sk_buff *skb;
  1510. int copied, rc;
  1511. /* put the autobinding in */
  1512. if (!ipxs->port) {
  1513. struct sockaddr_ipx uaddr;
  1514. uaddr.sipx_port = 0;
  1515. uaddr.sipx_network = 0;
  1516. #ifdef CONFIG_IPX_INTERN
  1517. rc = -ENETDOWN;
  1518. if (!ipxs->intrfc)
  1519. goto out; /* Someone zonked the iface */
  1520. memcpy(uaddr.sipx_node, ipxs->intrfc->if_node, IPX_NODE_LEN);
  1521. #endif /* CONFIG_IPX_INTERN */
  1522. rc = ipx_bind(sock, (struct sockaddr *)&uaddr,
  1523. sizeof(struct sockaddr_ipx));
  1524. if (rc)
  1525. goto out;
  1526. }
  1527. rc = -ENOTCONN;
  1528. if (sock_flag(sk, SOCK_ZAPPED))
  1529. goto out;
  1530. skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
  1531. flags & MSG_DONTWAIT, &rc);
  1532. if (!skb)
  1533. goto out;
  1534. ipx = ipx_hdr(skb);
  1535. copied = ntohs(ipx->ipx_pktsize) - sizeof(struct ipxhdr);
  1536. if (copied > size) {
  1537. copied = size;
  1538. msg->msg_flags |= MSG_TRUNC;
  1539. }
  1540. rc = skb_copy_datagram_iovec(skb, sizeof(struct ipxhdr), msg->msg_iov,
  1541. copied);
  1542. if (rc)
  1543. goto out_free;
  1544. if (skb->tstamp.tv64)
  1545. sk->sk_stamp = skb->tstamp;
  1546. msg->msg_namelen = sizeof(*sipx);
  1547. if (sipx) {
  1548. sipx->sipx_family = AF_IPX;
  1549. sipx->sipx_port = ipx->ipx_source.sock;
  1550. memcpy(sipx->sipx_node, ipx->ipx_source.node, IPX_NODE_LEN);
  1551. sipx->sipx_network = IPX_SKB_CB(skb)->ipx_source_net;
  1552. sipx->sipx_type = ipx->ipx_type;
  1553. sipx->sipx_zero = 0;
  1554. }
  1555. rc = copied;
  1556. out_free:
  1557. skb_free_datagram(sk, skb);
  1558. out:
  1559. return rc;
  1560. }
  1561. static int ipx_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1562. {
  1563. int rc = 0;
  1564. long amount = 0;
  1565. struct sock *sk = sock->sk;
  1566. void __user *argp = (void __user *)arg;
  1567. switch (cmd) {
  1568. case TIOCOUTQ:
  1569. amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  1570. if (amount < 0)
  1571. amount = 0;
  1572. rc = put_user(amount, (int __user *)argp);
  1573. break;
  1574. case TIOCINQ: {
  1575. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  1576. /* These two are safe on a single CPU system as only
  1577. * user tasks fiddle here */
  1578. if (skb)
  1579. amount = skb->len - sizeof(struct ipxhdr);
  1580. rc = put_user(amount, (int __user *)argp);
  1581. break;
  1582. }
  1583. case SIOCADDRT:
  1584. case SIOCDELRT:
  1585. rc = -EPERM;
  1586. if (capable(CAP_NET_ADMIN))
  1587. rc = ipxrtr_ioctl(cmd, argp);
  1588. break;
  1589. case SIOCSIFADDR:
  1590. case SIOCAIPXITFCRT:
  1591. case SIOCAIPXPRISLT:
  1592. rc = -EPERM;
  1593. if (!capable(CAP_NET_ADMIN))
  1594. break;
  1595. case SIOCGIFADDR:
  1596. rc = ipxitf_ioctl(cmd, argp);
  1597. break;
  1598. case SIOCIPXCFGDATA:
  1599. rc = ipxcfg_get_config_data(argp);
  1600. break;
  1601. case SIOCIPXNCPCONN:
  1602. /*
  1603. * This socket wants to take care of the NCP connection
  1604. * handed to us in arg.
  1605. */
  1606. rc = -EPERM;
  1607. if (!capable(CAP_NET_ADMIN))
  1608. break;
  1609. rc = get_user(ipx_sk(sk)->ipx_ncp_conn,
  1610. (const unsigned short __user *)argp);
  1611. break;
  1612. case SIOCGSTAMP:
  1613. rc = -EINVAL;
  1614. if (sk)
  1615. rc = sock_get_timestamp(sk, argp);
  1616. break;
  1617. case SIOCGIFDSTADDR:
  1618. case SIOCSIFDSTADDR:
  1619. case SIOCGIFBRDADDR:
  1620. case SIOCSIFBRDADDR:
  1621. case SIOCGIFNETMASK:
  1622. case SIOCSIFNETMASK:
  1623. rc = -EINVAL;
  1624. break;
  1625. default:
  1626. rc = -ENOIOCTLCMD;
  1627. break;
  1628. }
  1629. return rc;
  1630. }
  1631. #ifdef CONFIG_COMPAT
  1632. static int ipx_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1633. {
  1634. /*
  1635. * These 4 commands use same structure on 32bit and 64bit. Rest of IPX
  1636. * commands is handled by generic ioctl code. As these commands are
  1637. * SIOCPROTOPRIVATE..SIOCPROTOPRIVATE+3, they cannot be handled by generic
  1638. * code.
  1639. */
  1640. switch (cmd) {
  1641. case SIOCAIPXITFCRT:
  1642. case SIOCAIPXPRISLT:
  1643. case SIOCIPXCFGDATA:
  1644. case SIOCIPXNCPCONN:
  1645. return ipx_ioctl(sock, cmd, arg);
  1646. default:
  1647. return -ENOIOCTLCMD;
  1648. }
  1649. }
  1650. #endif
  1651. /*
  1652. * Socket family declarations
  1653. */
  1654. static struct net_proto_family ipx_family_ops = {
  1655. .family = PF_IPX,
  1656. .create = ipx_create,
  1657. .owner = THIS_MODULE,
  1658. };
  1659. static const struct proto_ops SOCKOPS_WRAPPED(ipx_dgram_ops) = {
  1660. .family = PF_IPX,
  1661. .owner = THIS_MODULE,
  1662. .release = ipx_release,
  1663. .bind = ipx_bind,
  1664. .connect = ipx_connect,
  1665. .socketpair = sock_no_socketpair,
  1666. .accept = sock_no_accept,
  1667. .getname = ipx_getname,
  1668. .poll = datagram_poll,
  1669. .ioctl = ipx_ioctl,
  1670. #ifdef CONFIG_COMPAT
  1671. .compat_ioctl = ipx_compat_ioctl,
  1672. #endif
  1673. .listen = sock_no_listen,
  1674. .shutdown = sock_no_shutdown, /* FIXME: support shutdown */
  1675. .setsockopt = ipx_setsockopt,
  1676. .getsockopt = ipx_getsockopt,
  1677. .sendmsg = ipx_sendmsg,
  1678. .recvmsg = ipx_recvmsg,
  1679. .mmap = sock_no_mmap,
  1680. .sendpage = sock_no_sendpage,
  1681. };
  1682. SOCKOPS_WRAP(ipx_dgram, PF_IPX);
  1683. static struct packet_type ipx_8023_packet_type = {
  1684. .type = __constant_htons(ETH_P_802_3),
  1685. .func = ipx_rcv,
  1686. };
  1687. static struct packet_type ipx_dix_packet_type = {
  1688. .type = __constant_htons(ETH_P_IPX),
  1689. .func = ipx_rcv,
  1690. };
  1691. static struct notifier_block ipx_dev_notifier = {
  1692. .notifier_call = ipxitf_device_event,
  1693. };
  1694. extern struct datalink_proto *make_EII_client(void);
  1695. extern void destroy_EII_client(struct datalink_proto *);
  1696. static unsigned char ipx_8022_type = 0xE0;
  1697. static unsigned char ipx_snap_id[5] = { 0x0, 0x0, 0x0, 0x81, 0x37 };
  1698. static char ipx_EII_err_msg[] __initdata =
  1699. KERN_CRIT "IPX: Unable to register with Ethernet II\n";
  1700. static char ipx_8023_err_msg[] __initdata =
  1701. KERN_CRIT "IPX: Unable to register with 802.3\n";
  1702. static char ipx_llc_err_msg[] __initdata =
  1703. KERN_CRIT "IPX: Unable to register with 802.2\n";
  1704. static char ipx_snap_err_msg[] __initdata =
  1705. KERN_CRIT "IPX: Unable to register with SNAP\n";
  1706. static int __init ipx_init(void)
  1707. {
  1708. int rc = proto_register(&ipx_proto, 1);
  1709. if (rc != 0)
  1710. goto out;
  1711. sock_register(&ipx_family_ops);
  1712. pEII_datalink = make_EII_client();
  1713. if (pEII_datalink)
  1714. dev_add_pack(&ipx_dix_packet_type);
  1715. else
  1716. printk(ipx_EII_err_msg);
  1717. p8023_datalink = make_8023_client();
  1718. if (p8023_datalink)
  1719. dev_add_pack(&ipx_8023_packet_type);
  1720. else
  1721. printk(ipx_8023_err_msg);
  1722. p8022_datalink = register_8022_client(ipx_8022_type, ipx_rcv);
  1723. if (!p8022_datalink)
  1724. printk(ipx_llc_err_msg);
  1725. pSNAP_datalink = register_snap_client(ipx_snap_id, ipx_rcv);
  1726. if (!pSNAP_datalink)
  1727. printk(ipx_snap_err_msg);
  1728. register_netdevice_notifier(&ipx_dev_notifier);
  1729. ipx_register_sysctl();
  1730. ipx_proc_init();
  1731. out:
  1732. return rc;
  1733. }
  1734. static void __exit ipx_proto_finito(void)
  1735. {
  1736. ipx_proc_exit();
  1737. ipx_unregister_sysctl();
  1738. unregister_netdevice_notifier(&ipx_dev_notifier);
  1739. ipxitf_cleanup();
  1740. if (pSNAP_datalink) {
  1741. unregister_snap_client(pSNAP_datalink);
  1742. pSNAP_datalink = NULL;
  1743. }
  1744. if (p8022_datalink) {
  1745. unregister_8022_client(p8022_datalink);
  1746. p8022_datalink = NULL;
  1747. }
  1748. dev_remove_pack(&ipx_8023_packet_type);
  1749. if (p8023_datalink) {
  1750. destroy_8023_client(p8023_datalink);
  1751. p8023_datalink = NULL;
  1752. }
  1753. dev_remove_pack(&ipx_dix_packet_type);
  1754. if (pEII_datalink) {
  1755. destroy_EII_client(pEII_datalink);
  1756. pEII_datalink = NULL;
  1757. }
  1758. proto_unregister(&ipx_proto);
  1759. sock_unregister(ipx_family_ops.family);
  1760. }
  1761. module_init(ipx_init);
  1762. module_exit(ipx_proto_finito);
  1763. MODULE_LICENSE("GPL");
  1764. MODULE_ALIAS_NETPROTO(PF_IPX);