af_ipx.c 50 KB

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