af_ipx.c 50 KB

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