af_ipx.c 50 KB

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