af_ipx.c 50 KB

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