af_ipx.c 50 KB

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