af_ipx.c 50 KB

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