af_ipx.c 50 KB

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