af_ipx.c 50 KB

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