udp.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The User Datagram Protocol (UDP).
  7. *
  8. * Version: $Id: udp.c,v 1.102 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  13. * Alan Cox, <Alan.Cox@linux.org>
  14. * Hirokazu Takahashi, <taka@valinux.co.jp>
  15. *
  16. * Fixes:
  17. * Alan Cox : verify_area() calls
  18. * Alan Cox : stopped close while in use off icmp
  19. * messages. Not a fix but a botch that
  20. * for udp at least is 'valid'.
  21. * Alan Cox : Fixed icmp handling properly
  22. * Alan Cox : Correct error for oversized datagrams
  23. * Alan Cox : Tidied select() semantics.
  24. * Alan Cox : udp_err() fixed properly, also now
  25. * select and read wake correctly on errors
  26. * Alan Cox : udp_send verify_area moved to avoid mem leak
  27. * Alan Cox : UDP can count its memory
  28. * Alan Cox : send to an unknown connection causes
  29. * an ECONNREFUSED off the icmp, but
  30. * does NOT close.
  31. * Alan Cox : Switched to new sk_buff handlers. No more backlog!
  32. * Alan Cox : Using generic datagram code. Even smaller and the PEEK
  33. * bug no longer crashes it.
  34. * Fred Van Kempen : Net2e support for sk->broadcast.
  35. * Alan Cox : Uses skb_free_datagram
  36. * Alan Cox : Added get/set sockopt support.
  37. * Alan Cox : Broadcasting without option set returns EACCES.
  38. * Alan Cox : No wakeup calls. Instead we now use the callbacks.
  39. * Alan Cox : Use ip_tos and ip_ttl
  40. * Alan Cox : SNMP Mibs
  41. * Alan Cox : MSG_DONTROUTE, and 0.0.0.0 support.
  42. * Matt Dillon : UDP length checks.
  43. * Alan Cox : Smarter af_inet used properly.
  44. * Alan Cox : Use new kernel side addressing.
  45. * Alan Cox : Incorrect return on truncated datagram receive.
  46. * Arnt Gulbrandsen : New udp_send and stuff
  47. * Alan Cox : Cache last socket
  48. * Alan Cox : Route cache
  49. * Jon Peatfield : Minor efficiency fix to sendto().
  50. * Mike Shaver : RFC1122 checks.
  51. * Alan Cox : Nonblocking error fix.
  52. * Willy Konynenberg : Transparent proxying support.
  53. * Mike McLagan : Routing by source
  54. * David S. Miller : New socket lookup architecture.
  55. * Last socket cache retained as it
  56. * does have a high hit rate.
  57. * Olaf Kirch : Don't linearise iovec on sendmsg.
  58. * Andi Kleen : Some cleanups, cache destination entry
  59. * for connect.
  60. * Vitaly E. Lavrov : Transparent proxy revived after year coma.
  61. * Melvin Smith : Check msg_name not msg_namelen in sendto(),
  62. * return ENOTCONN for unconnected sockets (POSIX)
  63. * Janos Farkas : don't deliver multi/broadcasts to a different
  64. * bound-to-device socket
  65. * Hirokazu Takahashi : HW checksumming for outgoing UDP
  66. * datagrams.
  67. * Hirokazu Takahashi : sendfile() on UDP works now.
  68. * Arnaldo C. Melo : convert /proc/net/udp to seq_file
  69. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  70. * Alexey Kuznetsov: allow both IPv4 and IPv6 sockets to bind
  71. * a single port at the same time.
  72. * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
  73. *
  74. *
  75. * This program is free software; you can redistribute it and/or
  76. * modify it under the terms of the GNU General Public License
  77. * as published by the Free Software Foundation; either version
  78. * 2 of the License, or (at your option) any later version.
  79. */
  80. #include <asm/system.h>
  81. #include <asm/uaccess.h>
  82. #include <asm/ioctls.h>
  83. #include <linux/types.h>
  84. #include <linux/fcntl.h>
  85. #include <linux/module.h>
  86. #include <linux/socket.h>
  87. #include <linux/sockios.h>
  88. #include <linux/in.h>
  89. #include <linux/errno.h>
  90. #include <linux/timer.h>
  91. #include <linux/mm.h>
  92. #include <linux/config.h>
  93. #include <linux/inet.h>
  94. #include <linux/ipv6.h>
  95. #include <linux/netdevice.h>
  96. #include <net/snmp.h>
  97. #include <net/tcp.h>
  98. #include <net/protocol.h>
  99. #include <linux/skbuff.h>
  100. #include <linux/proc_fs.h>
  101. #include <linux/seq_file.h>
  102. #include <net/sock.h>
  103. #include <net/udp.h>
  104. #include <net/icmp.h>
  105. #include <net/route.h>
  106. #include <net/inet_common.h>
  107. #include <net/checksum.h>
  108. #include <net/xfrm.h>
  109. /*
  110. * Snmp MIB for the UDP layer
  111. */
  112. DEFINE_SNMP_STAT(struct udp_mib, udp_statistics);
  113. struct hlist_head udp_hash[UDP_HTABLE_SIZE];
  114. DEFINE_RWLOCK(udp_hash_lock);
  115. /* Shared by v4/v6 udp. */
  116. int udp_port_rover;
  117. static int udp_v4_get_port(struct sock *sk, unsigned short snum)
  118. {
  119. struct hlist_node *node;
  120. struct sock *sk2;
  121. struct inet_sock *inet = inet_sk(sk);
  122. write_lock_bh(&udp_hash_lock);
  123. if (snum == 0) {
  124. int best_size_so_far, best, result, i;
  125. if (udp_port_rover > sysctl_local_port_range[1] ||
  126. udp_port_rover < sysctl_local_port_range[0])
  127. udp_port_rover = sysctl_local_port_range[0];
  128. best_size_so_far = 32767;
  129. best = result = udp_port_rover;
  130. for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
  131. struct hlist_head *list;
  132. int size;
  133. list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
  134. if (hlist_empty(list)) {
  135. if (result > sysctl_local_port_range[1])
  136. result = sysctl_local_port_range[0] +
  137. ((result - sysctl_local_port_range[0]) &
  138. (UDP_HTABLE_SIZE - 1));
  139. goto gotit;
  140. }
  141. size = 0;
  142. sk_for_each(sk2, node, list)
  143. if (++size >= best_size_so_far)
  144. goto next;
  145. best_size_so_far = size;
  146. best = result;
  147. next:;
  148. }
  149. result = best;
  150. for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
  151. if (result > sysctl_local_port_range[1])
  152. result = sysctl_local_port_range[0]
  153. + ((result - sysctl_local_port_range[0]) &
  154. (UDP_HTABLE_SIZE - 1));
  155. if (!udp_lport_inuse(result))
  156. break;
  157. }
  158. if (i >= (1 << 16) / UDP_HTABLE_SIZE)
  159. goto fail;
  160. gotit:
  161. udp_port_rover = snum = result;
  162. } else {
  163. sk_for_each(sk2, node,
  164. &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
  165. struct inet_sock *inet2 = inet_sk(sk2);
  166. if (inet2->num == snum &&
  167. sk2 != sk &&
  168. !ipv6_only_sock(sk2) &&
  169. (!sk2->sk_bound_dev_if ||
  170. !sk->sk_bound_dev_if ||
  171. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
  172. (!inet2->rcv_saddr ||
  173. !inet->rcv_saddr ||
  174. inet2->rcv_saddr == inet->rcv_saddr) &&
  175. (!sk2->sk_reuse || !sk->sk_reuse))
  176. goto fail;
  177. }
  178. }
  179. inet->num = snum;
  180. if (sk_unhashed(sk)) {
  181. struct hlist_head *h = &udp_hash[snum & (UDP_HTABLE_SIZE - 1)];
  182. sk_add_node(sk, h);
  183. sock_prot_inc_use(sk->sk_prot);
  184. }
  185. write_unlock_bh(&udp_hash_lock);
  186. return 0;
  187. fail:
  188. write_unlock_bh(&udp_hash_lock);
  189. return 1;
  190. }
  191. static void udp_v4_hash(struct sock *sk)
  192. {
  193. BUG();
  194. }
  195. static void udp_v4_unhash(struct sock *sk)
  196. {
  197. write_lock_bh(&udp_hash_lock);
  198. if (sk_del_node_init(sk)) {
  199. inet_sk(sk)->num = 0;
  200. sock_prot_dec_use(sk->sk_prot);
  201. }
  202. write_unlock_bh(&udp_hash_lock);
  203. }
  204. /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
  205. * harder than this. -DaveM
  206. */
  207. static struct sock *udp_v4_lookup_longway(u32 saddr, u16 sport,
  208. u32 daddr, u16 dport, int dif)
  209. {
  210. struct sock *sk, *result = NULL;
  211. struct hlist_node *node;
  212. unsigned short hnum = ntohs(dport);
  213. int badness = -1;
  214. sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
  215. struct inet_sock *inet = inet_sk(sk);
  216. if (inet->num == hnum && !ipv6_only_sock(sk)) {
  217. int score = (sk->sk_family == PF_INET ? 1 : 0);
  218. if (inet->rcv_saddr) {
  219. if (inet->rcv_saddr != daddr)
  220. continue;
  221. score+=2;
  222. }
  223. if (inet->daddr) {
  224. if (inet->daddr != saddr)
  225. continue;
  226. score+=2;
  227. }
  228. if (inet->dport) {
  229. if (inet->dport != sport)
  230. continue;
  231. score+=2;
  232. }
  233. if (sk->sk_bound_dev_if) {
  234. if (sk->sk_bound_dev_if != dif)
  235. continue;
  236. score+=2;
  237. }
  238. if(score == 9) {
  239. result = sk;
  240. break;
  241. } else if(score > badness) {
  242. result = sk;
  243. badness = score;
  244. }
  245. }
  246. }
  247. return result;
  248. }
  249. static __inline__ struct sock *udp_v4_lookup(u32 saddr, u16 sport,
  250. u32 daddr, u16 dport, int dif)
  251. {
  252. struct sock *sk;
  253. read_lock(&udp_hash_lock);
  254. sk = udp_v4_lookup_longway(saddr, sport, daddr, dport, dif);
  255. if (sk)
  256. sock_hold(sk);
  257. read_unlock(&udp_hash_lock);
  258. return sk;
  259. }
  260. static inline struct sock *udp_v4_mcast_next(struct sock *sk,
  261. u16 loc_port, u32 loc_addr,
  262. u16 rmt_port, u32 rmt_addr,
  263. int dif)
  264. {
  265. struct hlist_node *node;
  266. struct sock *s = sk;
  267. unsigned short hnum = ntohs(loc_port);
  268. sk_for_each_from(s, node) {
  269. struct inet_sock *inet = inet_sk(s);
  270. if (inet->num != hnum ||
  271. (inet->daddr && inet->daddr != rmt_addr) ||
  272. (inet->dport != rmt_port && inet->dport) ||
  273. (inet->rcv_saddr && inet->rcv_saddr != loc_addr) ||
  274. ipv6_only_sock(s) ||
  275. (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
  276. continue;
  277. if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
  278. continue;
  279. goto found;
  280. }
  281. s = NULL;
  282. found:
  283. return s;
  284. }
  285. /*
  286. * This routine is called by the ICMP module when it gets some
  287. * sort of error condition. If err < 0 then the socket should
  288. * be closed and the error returned to the user. If err > 0
  289. * it's just the icmp type << 8 | icmp code.
  290. * Header points to the ip header of the error packet. We move
  291. * on past this. Then (as it used to claim before adjustment)
  292. * header points to the first 8 bytes of the udp header. We need
  293. * to find the appropriate port.
  294. */
  295. void udp_err(struct sk_buff *skb, u32 info)
  296. {
  297. struct inet_sock *inet;
  298. struct iphdr *iph = (struct iphdr*)skb->data;
  299. struct udphdr *uh = (struct udphdr*)(skb->data+(iph->ihl<<2));
  300. int type = skb->h.icmph->type;
  301. int code = skb->h.icmph->code;
  302. struct sock *sk;
  303. int harderr;
  304. int err;
  305. sk = udp_v4_lookup(iph->daddr, uh->dest, iph->saddr, uh->source, skb->dev->ifindex);
  306. if (sk == NULL) {
  307. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  308. return; /* No socket for error */
  309. }
  310. err = 0;
  311. harderr = 0;
  312. inet = inet_sk(sk);
  313. switch (type) {
  314. default:
  315. case ICMP_TIME_EXCEEDED:
  316. err = EHOSTUNREACH;
  317. break;
  318. case ICMP_SOURCE_QUENCH:
  319. goto out;
  320. case ICMP_PARAMETERPROB:
  321. err = EPROTO;
  322. harderr = 1;
  323. break;
  324. case ICMP_DEST_UNREACH:
  325. if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
  326. if (inet->pmtudisc != IP_PMTUDISC_DONT) {
  327. err = EMSGSIZE;
  328. harderr = 1;
  329. break;
  330. }
  331. goto out;
  332. }
  333. err = EHOSTUNREACH;
  334. if (code <= NR_ICMP_UNREACH) {
  335. harderr = icmp_err_convert[code].fatal;
  336. err = icmp_err_convert[code].errno;
  337. }
  338. break;
  339. }
  340. /*
  341. * RFC1122: OK. Passes ICMP errors back to application, as per
  342. * 4.1.3.3.
  343. */
  344. if (!inet->recverr) {
  345. if (!harderr || sk->sk_state != TCP_ESTABLISHED)
  346. goto out;
  347. } else {
  348. ip_icmp_error(sk, skb, err, uh->dest, info, (u8*)(uh+1));
  349. }
  350. sk->sk_err = err;
  351. sk->sk_error_report(sk);
  352. out:
  353. sock_put(sk);
  354. }
  355. /*
  356. * Throw away all pending data and cancel the corking. Socket is locked.
  357. */
  358. static void udp_flush_pending_frames(struct sock *sk)
  359. {
  360. struct udp_sock *up = udp_sk(sk);
  361. if (up->pending) {
  362. up->len = 0;
  363. up->pending = 0;
  364. ip_flush_pending_frames(sk);
  365. }
  366. }
  367. /*
  368. * Push out all pending data as one UDP datagram. Socket is locked.
  369. */
  370. static int udp_push_pending_frames(struct sock *sk, struct udp_sock *up)
  371. {
  372. struct inet_sock *inet = inet_sk(sk);
  373. struct flowi *fl = &inet->cork.fl;
  374. struct sk_buff *skb;
  375. struct udphdr *uh;
  376. int err = 0;
  377. /* Grab the skbuff where UDP header space exists. */
  378. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  379. goto out;
  380. /*
  381. * Create a UDP header
  382. */
  383. uh = skb->h.uh;
  384. uh->source = fl->fl_ip_sport;
  385. uh->dest = fl->fl_ip_dport;
  386. uh->len = htons(up->len);
  387. uh->check = 0;
  388. if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
  389. skb->ip_summed = CHECKSUM_NONE;
  390. goto send;
  391. }
  392. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  393. /*
  394. * Only one fragment on the socket.
  395. */
  396. if (skb->ip_summed == CHECKSUM_HW) {
  397. skb->csum = offsetof(struct udphdr, check);
  398. uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
  399. up->len, IPPROTO_UDP, 0);
  400. } else {
  401. skb->csum = csum_partial((char *)uh,
  402. sizeof(struct udphdr), skb->csum);
  403. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
  404. up->len, IPPROTO_UDP, skb->csum);
  405. if (uh->check == 0)
  406. uh->check = -1;
  407. }
  408. } else {
  409. unsigned int csum = 0;
  410. /*
  411. * HW-checksum won't work as there are two or more
  412. * fragments on the socket so that all csums of sk_buffs
  413. * should be together.
  414. */
  415. if (skb->ip_summed == CHECKSUM_HW) {
  416. int offset = (unsigned char *)uh - skb->data;
  417. skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
  418. skb->ip_summed = CHECKSUM_NONE;
  419. } else {
  420. skb->csum = csum_partial((char *)uh,
  421. sizeof(struct udphdr), skb->csum);
  422. }
  423. skb_queue_walk(&sk->sk_write_queue, skb) {
  424. csum = csum_add(csum, skb->csum);
  425. }
  426. uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
  427. up->len, IPPROTO_UDP, csum);
  428. if (uh->check == 0)
  429. uh->check = -1;
  430. }
  431. send:
  432. err = ip_push_pending_frames(sk);
  433. out:
  434. up->len = 0;
  435. up->pending = 0;
  436. return err;
  437. }
  438. static unsigned short udp_check(struct udphdr *uh, int len, unsigned long saddr, unsigned long daddr, unsigned long base)
  439. {
  440. return(csum_tcpudp_magic(saddr, daddr, len, IPPROTO_UDP, base));
  441. }
  442. int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  443. size_t len)
  444. {
  445. struct inet_sock *inet = inet_sk(sk);
  446. struct udp_sock *up = udp_sk(sk);
  447. int ulen = len;
  448. struct ipcm_cookie ipc;
  449. struct rtable *rt = NULL;
  450. int free = 0;
  451. int connected = 0;
  452. u32 daddr, faddr, saddr;
  453. u16 dport;
  454. u8 tos;
  455. int err;
  456. int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
  457. if (len > 0xFFFF)
  458. return -EMSGSIZE;
  459. /*
  460. * Check the flags.
  461. */
  462. if (msg->msg_flags&MSG_OOB) /* Mirror BSD error message compatibility */
  463. return -EOPNOTSUPP;
  464. ipc.opt = NULL;
  465. if (up->pending) {
  466. /*
  467. * There are pending frames.
  468. * The socket lock must be held while it's corked.
  469. */
  470. lock_sock(sk);
  471. if (likely(up->pending)) {
  472. if (unlikely(up->pending != AF_INET)) {
  473. release_sock(sk);
  474. return -EINVAL;
  475. }
  476. goto do_append_data;
  477. }
  478. release_sock(sk);
  479. }
  480. ulen += sizeof(struct udphdr);
  481. /*
  482. * Get and verify the address.
  483. */
  484. if (msg->msg_name) {
  485. struct sockaddr_in * usin = (struct sockaddr_in*)msg->msg_name;
  486. if (msg->msg_namelen < sizeof(*usin))
  487. return -EINVAL;
  488. if (usin->sin_family != AF_INET) {
  489. if (usin->sin_family != AF_UNSPEC)
  490. return -EAFNOSUPPORT;
  491. }
  492. daddr = usin->sin_addr.s_addr;
  493. dport = usin->sin_port;
  494. if (dport == 0)
  495. return -EINVAL;
  496. } else {
  497. if (sk->sk_state != TCP_ESTABLISHED)
  498. return -EDESTADDRREQ;
  499. daddr = inet->daddr;
  500. dport = inet->dport;
  501. /* Open fast path for connected socket.
  502. Route will not be used, if at least one option is set.
  503. */
  504. connected = 1;
  505. }
  506. ipc.addr = inet->saddr;
  507. ipc.oif = sk->sk_bound_dev_if;
  508. if (msg->msg_controllen) {
  509. err = ip_cmsg_send(msg, &ipc);
  510. if (err)
  511. return err;
  512. if (ipc.opt)
  513. free = 1;
  514. connected = 0;
  515. }
  516. if (!ipc.opt)
  517. ipc.opt = inet->opt;
  518. saddr = ipc.addr;
  519. ipc.addr = faddr = daddr;
  520. if (ipc.opt && ipc.opt->srr) {
  521. if (!daddr)
  522. return -EINVAL;
  523. faddr = ipc.opt->faddr;
  524. connected = 0;
  525. }
  526. tos = RT_TOS(inet->tos);
  527. if (sock_flag(sk, SOCK_LOCALROUTE) ||
  528. (msg->msg_flags & MSG_DONTROUTE) ||
  529. (ipc.opt && ipc.opt->is_strictroute)) {
  530. tos |= RTO_ONLINK;
  531. connected = 0;
  532. }
  533. if (MULTICAST(daddr)) {
  534. if (!ipc.oif)
  535. ipc.oif = inet->mc_index;
  536. if (!saddr)
  537. saddr = inet->mc_addr;
  538. connected = 0;
  539. }
  540. if (connected)
  541. rt = (struct rtable*)sk_dst_check(sk, 0);
  542. if (rt == NULL) {
  543. struct flowi fl = { .oif = ipc.oif,
  544. .nl_u = { .ip4_u =
  545. { .daddr = faddr,
  546. .saddr = saddr,
  547. .tos = tos } },
  548. .proto = IPPROTO_UDP,
  549. .uli_u = { .ports =
  550. { .sport = inet->sport,
  551. .dport = dport } } };
  552. err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
  553. if (err)
  554. goto out;
  555. err = -EACCES;
  556. if ((rt->rt_flags & RTCF_BROADCAST) &&
  557. !sock_flag(sk, SOCK_BROADCAST))
  558. goto out;
  559. if (connected)
  560. sk_dst_set(sk, dst_clone(&rt->u.dst));
  561. }
  562. if (msg->msg_flags&MSG_CONFIRM)
  563. goto do_confirm;
  564. back_from_confirm:
  565. saddr = rt->rt_src;
  566. if (!ipc.addr)
  567. daddr = ipc.addr = rt->rt_dst;
  568. lock_sock(sk);
  569. if (unlikely(up->pending)) {
  570. /* The socket is already corked while preparing it. */
  571. /* ... which is an evident application bug. --ANK */
  572. release_sock(sk);
  573. LIMIT_NETDEBUG(printk(KERN_DEBUG "udp cork app bug 2\n"));
  574. err = -EINVAL;
  575. goto out;
  576. }
  577. /*
  578. * Now cork the socket to pend data.
  579. */
  580. inet->cork.fl.fl4_dst = daddr;
  581. inet->cork.fl.fl_ip_dport = dport;
  582. inet->cork.fl.fl4_src = saddr;
  583. inet->cork.fl.fl_ip_sport = inet->sport;
  584. up->pending = AF_INET;
  585. do_append_data:
  586. up->len += ulen;
  587. err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen,
  588. sizeof(struct udphdr), &ipc, rt,
  589. corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
  590. if (err)
  591. udp_flush_pending_frames(sk);
  592. else if (!corkreq)
  593. err = udp_push_pending_frames(sk, up);
  594. release_sock(sk);
  595. out:
  596. ip_rt_put(rt);
  597. if (free)
  598. kfree(ipc.opt);
  599. if (!err) {
  600. UDP_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
  601. return len;
  602. }
  603. return err;
  604. do_confirm:
  605. dst_confirm(&rt->u.dst);
  606. if (!(msg->msg_flags&MSG_PROBE) || len)
  607. goto back_from_confirm;
  608. err = 0;
  609. goto out;
  610. }
  611. static int udp_sendpage(struct sock *sk, struct page *page, int offset,
  612. size_t size, int flags)
  613. {
  614. struct udp_sock *up = udp_sk(sk);
  615. int ret;
  616. if (!up->pending) {
  617. struct msghdr msg = { .msg_flags = flags|MSG_MORE };
  618. /* Call udp_sendmsg to specify destination address which
  619. * sendpage interface can't pass.
  620. * This will succeed only when the socket is connected.
  621. */
  622. ret = udp_sendmsg(NULL, sk, &msg, 0);
  623. if (ret < 0)
  624. return ret;
  625. }
  626. lock_sock(sk);
  627. if (unlikely(!up->pending)) {
  628. release_sock(sk);
  629. LIMIT_NETDEBUG(printk(KERN_DEBUG "udp cork app bug 3\n"));
  630. return -EINVAL;
  631. }
  632. ret = ip_append_page(sk, page, offset, size, flags);
  633. if (ret == -EOPNOTSUPP) {
  634. release_sock(sk);
  635. return sock_no_sendpage(sk->sk_socket, page, offset,
  636. size, flags);
  637. }
  638. if (ret < 0) {
  639. udp_flush_pending_frames(sk);
  640. goto out;
  641. }
  642. up->len += size;
  643. if (!(up->corkflag || (flags&MSG_MORE)))
  644. ret = udp_push_pending_frames(sk, up);
  645. if (!ret)
  646. ret = size;
  647. out:
  648. release_sock(sk);
  649. return ret;
  650. }
  651. /*
  652. * IOCTL requests applicable to the UDP protocol
  653. */
  654. int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  655. {
  656. switch(cmd)
  657. {
  658. case SIOCOUTQ:
  659. {
  660. int amount = atomic_read(&sk->sk_wmem_alloc);
  661. return put_user(amount, (int __user *)arg);
  662. }
  663. case SIOCINQ:
  664. {
  665. struct sk_buff *skb;
  666. unsigned long amount;
  667. amount = 0;
  668. spin_lock_bh(&sk->sk_receive_queue.lock);
  669. skb = skb_peek(&sk->sk_receive_queue);
  670. if (skb != NULL) {
  671. /*
  672. * We will only return the amount
  673. * of this packet since that is all
  674. * that will be read.
  675. */
  676. amount = skb->len - sizeof(struct udphdr);
  677. }
  678. spin_unlock_bh(&sk->sk_receive_queue.lock);
  679. return put_user(amount, (int __user *)arg);
  680. }
  681. default:
  682. return -ENOIOCTLCMD;
  683. }
  684. return(0);
  685. }
  686. static __inline__ int __udp_checksum_complete(struct sk_buff *skb)
  687. {
  688. return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
  689. }
  690. static __inline__ int udp_checksum_complete(struct sk_buff *skb)
  691. {
  692. return skb->ip_summed != CHECKSUM_UNNECESSARY &&
  693. __udp_checksum_complete(skb);
  694. }
  695. /*
  696. * This should be easy, if there is something there we
  697. * return it, otherwise we block.
  698. */
  699. static int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  700. size_t len, int noblock, int flags, int *addr_len)
  701. {
  702. struct inet_sock *inet = inet_sk(sk);
  703. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  704. struct sk_buff *skb;
  705. int copied, err;
  706. /*
  707. * Check any passed addresses
  708. */
  709. if (addr_len)
  710. *addr_len=sizeof(*sin);
  711. if (flags & MSG_ERRQUEUE)
  712. return ip_recv_error(sk, msg, len);
  713. try_again:
  714. skb = skb_recv_datagram(sk, flags, noblock, &err);
  715. if (!skb)
  716. goto out;
  717. copied = skb->len - sizeof(struct udphdr);
  718. if (copied > len) {
  719. copied = len;
  720. msg->msg_flags |= MSG_TRUNC;
  721. }
  722. if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
  723. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
  724. copied);
  725. } else if (msg->msg_flags&MSG_TRUNC) {
  726. if (__udp_checksum_complete(skb))
  727. goto csum_copy_err;
  728. err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
  729. copied);
  730. } else {
  731. err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
  732. if (err == -EINVAL)
  733. goto csum_copy_err;
  734. }
  735. if (err)
  736. goto out_free;
  737. sock_recv_timestamp(msg, sk, skb);
  738. /* Copy the address. */
  739. if (sin)
  740. {
  741. sin->sin_family = AF_INET;
  742. sin->sin_port = skb->h.uh->source;
  743. sin->sin_addr.s_addr = skb->nh.iph->saddr;
  744. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  745. }
  746. if (inet->cmsg_flags)
  747. ip_cmsg_recv(msg, skb);
  748. err = copied;
  749. if (flags & MSG_TRUNC)
  750. err = skb->len - sizeof(struct udphdr);
  751. out_free:
  752. skb_free_datagram(sk, skb);
  753. out:
  754. return err;
  755. csum_copy_err:
  756. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  757. /* Clear queue. */
  758. if (flags&MSG_PEEK) {
  759. int clear = 0;
  760. spin_lock_bh(&sk->sk_receive_queue.lock);
  761. if (skb == skb_peek(&sk->sk_receive_queue)) {
  762. __skb_unlink(skb, &sk->sk_receive_queue);
  763. clear = 1;
  764. }
  765. spin_unlock_bh(&sk->sk_receive_queue.lock);
  766. if (clear)
  767. kfree_skb(skb);
  768. }
  769. skb_free_datagram(sk, skb);
  770. if (noblock)
  771. return -EAGAIN;
  772. goto try_again;
  773. }
  774. int udp_disconnect(struct sock *sk, int flags)
  775. {
  776. struct inet_sock *inet = inet_sk(sk);
  777. /*
  778. * 1003.1g - break association.
  779. */
  780. sk->sk_state = TCP_CLOSE;
  781. inet->daddr = 0;
  782. inet->dport = 0;
  783. sk->sk_bound_dev_if = 0;
  784. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  785. inet_reset_saddr(sk);
  786. if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
  787. sk->sk_prot->unhash(sk);
  788. inet->sport = 0;
  789. }
  790. sk_dst_reset(sk);
  791. return 0;
  792. }
  793. static void udp_close(struct sock *sk, long timeout)
  794. {
  795. sk_common_release(sk);
  796. }
  797. /* return:
  798. * 1 if the the UDP system should process it
  799. * 0 if we should drop this packet
  800. * -1 if it should get processed by xfrm4_rcv_encap
  801. */
  802. static int udp_encap_rcv(struct sock * sk, struct sk_buff *skb)
  803. {
  804. #ifndef CONFIG_XFRM
  805. return 1;
  806. #else
  807. struct udp_sock *up = udp_sk(sk);
  808. struct udphdr *uh = skb->h.uh;
  809. struct iphdr *iph;
  810. int iphlen, len;
  811. __u8 *udpdata = (__u8 *)uh + sizeof(struct udphdr);
  812. __u32 *udpdata32 = (__u32 *)udpdata;
  813. __u16 encap_type = up->encap_type;
  814. /* if we're overly short, let UDP handle it */
  815. if (udpdata > skb->tail)
  816. return 1;
  817. /* if this is not encapsulated socket, then just return now */
  818. if (!encap_type)
  819. return 1;
  820. len = skb->tail - udpdata;
  821. switch (encap_type) {
  822. default:
  823. case UDP_ENCAP_ESPINUDP:
  824. /* Check if this is a keepalive packet. If so, eat it. */
  825. if (len == 1 && udpdata[0] == 0xff) {
  826. return 0;
  827. } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0 ) {
  828. /* ESP Packet without Non-ESP header */
  829. len = sizeof(struct udphdr);
  830. } else
  831. /* Must be an IKE packet.. pass it through */
  832. return 1;
  833. break;
  834. case UDP_ENCAP_ESPINUDP_NON_IKE:
  835. /* Check if this is a keepalive packet. If so, eat it. */
  836. if (len == 1 && udpdata[0] == 0xff) {
  837. return 0;
  838. } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
  839. udpdata32[0] == 0 && udpdata32[1] == 0) {
  840. /* ESP Packet with Non-IKE marker */
  841. len = sizeof(struct udphdr) + 2 * sizeof(u32);
  842. } else
  843. /* Must be an IKE packet.. pass it through */
  844. return 1;
  845. break;
  846. }
  847. /* At this point we are sure that this is an ESPinUDP packet,
  848. * so we need to remove 'len' bytes from the packet (the UDP
  849. * header and optional ESP marker bytes) and then modify the
  850. * protocol to ESP, and then call into the transform receiver.
  851. */
  852. if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  853. return 0;
  854. /* Now we can update and verify the packet length... */
  855. iph = skb->nh.iph;
  856. iphlen = iph->ihl << 2;
  857. iph->tot_len = htons(ntohs(iph->tot_len) - len);
  858. if (skb->len < iphlen + len) {
  859. /* packet is too small!?! */
  860. return 0;
  861. }
  862. /* pull the data buffer up to the ESP header and set the
  863. * transport header to point to ESP. Keep UDP on the stack
  864. * for later.
  865. */
  866. skb->h.raw = skb_pull(skb, len);
  867. /* modify the protocol (it's ESP!) */
  868. iph->protocol = IPPROTO_ESP;
  869. /* and let the caller know to send this into the ESP processor... */
  870. return -1;
  871. #endif
  872. }
  873. /* returns:
  874. * -1: error
  875. * 0: success
  876. * >0: "udp encap" protocol resubmission
  877. *
  878. * Note that in the success and error cases, the skb is assumed to
  879. * have either been requeued or freed.
  880. */
  881. static int udp_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
  882. {
  883. struct udp_sock *up = udp_sk(sk);
  884. /*
  885. * Charge it to the socket, dropping if the queue is full.
  886. */
  887. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
  888. kfree_skb(skb);
  889. return -1;
  890. }
  891. if (up->encap_type) {
  892. /*
  893. * This is an encapsulation socket, so let's see if this is
  894. * an encapsulated packet.
  895. * If it's a keepalive packet, then just eat it.
  896. * If it's an encapsulateed packet, then pass it to the
  897. * IPsec xfrm input and return the response
  898. * appropriately. Otherwise, just fall through and
  899. * pass this up the UDP socket.
  900. */
  901. int ret;
  902. ret = udp_encap_rcv(sk, skb);
  903. if (ret == 0) {
  904. /* Eat the packet .. */
  905. kfree_skb(skb);
  906. return 0;
  907. }
  908. if (ret < 0) {
  909. /* process the ESP packet */
  910. ret = xfrm4_rcv_encap(skb, up->encap_type);
  911. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
  912. return -ret;
  913. }
  914. /* FALLTHROUGH -- it's a UDP Packet */
  915. }
  916. if (sk->sk_filter && skb->ip_summed != CHECKSUM_UNNECESSARY) {
  917. if (__udp_checksum_complete(skb)) {
  918. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  919. kfree_skb(skb);
  920. return -1;
  921. }
  922. skb->ip_summed = CHECKSUM_UNNECESSARY;
  923. }
  924. if (sock_queue_rcv_skb(sk,skb)<0) {
  925. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  926. kfree_skb(skb);
  927. return -1;
  928. }
  929. UDP_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
  930. return 0;
  931. }
  932. /*
  933. * Multicasts and broadcasts go to each listener.
  934. *
  935. * Note: called only from the BH handler context,
  936. * so we don't need to lock the hashes.
  937. */
  938. static int udp_v4_mcast_deliver(struct sk_buff *skb, struct udphdr *uh,
  939. u32 saddr, u32 daddr)
  940. {
  941. struct sock *sk;
  942. int dif;
  943. read_lock(&udp_hash_lock);
  944. sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
  945. dif = skb->dev->ifindex;
  946. sk = udp_v4_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
  947. if (sk) {
  948. struct sock *sknext = NULL;
  949. do {
  950. struct sk_buff *skb1 = skb;
  951. sknext = udp_v4_mcast_next(sk_next(sk), uh->dest, daddr,
  952. uh->source, saddr, dif);
  953. if(sknext)
  954. skb1 = skb_clone(skb, GFP_ATOMIC);
  955. if(skb1) {
  956. int ret = udp_queue_rcv_skb(sk, skb1);
  957. if (ret > 0)
  958. /* we should probably re-process instead
  959. * of dropping packets here. */
  960. kfree_skb(skb1);
  961. }
  962. sk = sknext;
  963. } while(sknext);
  964. } else
  965. kfree_skb(skb);
  966. read_unlock(&udp_hash_lock);
  967. return 0;
  968. }
  969. /* Initialize UDP checksum. If exited with zero value (success),
  970. * CHECKSUM_UNNECESSARY means, that no more checks are required.
  971. * Otherwise, csum completion requires chacksumming packet body,
  972. * including udp header and folding it to skb->csum.
  973. */
  974. static int udp_checksum_init(struct sk_buff *skb, struct udphdr *uh,
  975. unsigned short ulen, u32 saddr, u32 daddr)
  976. {
  977. if (uh->check == 0) {
  978. skb->ip_summed = CHECKSUM_UNNECESSARY;
  979. } else if (skb->ip_summed == CHECKSUM_HW) {
  980. skb->ip_summed = CHECKSUM_UNNECESSARY;
  981. if (!udp_check(uh, ulen, saddr, daddr, skb->csum))
  982. return 0;
  983. LIMIT_NETDEBUG(printk(KERN_DEBUG "udp v4 hw csum failure.\n"));
  984. skb->ip_summed = CHECKSUM_NONE;
  985. }
  986. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  987. skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  988. /* Probably, we should checksum udp header (it should be in cache
  989. * in any case) and data in tiny packets (< rx copybreak).
  990. */
  991. return 0;
  992. }
  993. /*
  994. * All we need to do is get the socket, and then do a checksum.
  995. */
  996. int udp_rcv(struct sk_buff *skb)
  997. {
  998. struct sock *sk;
  999. struct udphdr *uh;
  1000. unsigned short ulen;
  1001. struct rtable *rt = (struct rtable*)skb->dst;
  1002. u32 saddr = skb->nh.iph->saddr;
  1003. u32 daddr = skb->nh.iph->daddr;
  1004. int len = skb->len;
  1005. /*
  1006. * Validate the packet and the UDP length.
  1007. */
  1008. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  1009. goto no_header;
  1010. uh = skb->h.uh;
  1011. ulen = ntohs(uh->len);
  1012. if (ulen > len || ulen < sizeof(*uh))
  1013. goto short_packet;
  1014. if (pskb_trim(skb, ulen))
  1015. goto short_packet;
  1016. if (udp_checksum_init(skb, uh, ulen, saddr, daddr) < 0)
  1017. goto csum_error;
  1018. if(rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
  1019. return udp_v4_mcast_deliver(skb, uh, saddr, daddr);
  1020. sk = udp_v4_lookup(saddr, uh->source, daddr, uh->dest, skb->dev->ifindex);
  1021. if (sk != NULL) {
  1022. int ret = udp_queue_rcv_skb(sk, skb);
  1023. sock_put(sk);
  1024. /* a return value > 0 means to resubmit the input, but
  1025. * it it wants the return to be -protocol, or 0
  1026. */
  1027. if (ret > 0)
  1028. return -ret;
  1029. return 0;
  1030. }
  1031. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1032. goto drop;
  1033. /* No socket. Drop packet silently, if checksum is wrong */
  1034. if (udp_checksum_complete(skb))
  1035. goto csum_error;
  1036. UDP_INC_STATS_BH(UDP_MIB_NOPORTS);
  1037. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
  1038. /*
  1039. * Hmm. We got an UDP packet to a port to which we
  1040. * don't wanna listen. Ignore it.
  1041. */
  1042. kfree_skb(skb);
  1043. return(0);
  1044. short_packet:
  1045. LIMIT_NETDEBUG(printk(KERN_DEBUG "UDP: short packet: From %u.%u.%u.%u:%u %d/%d to %u.%u.%u.%u:%u\n",
  1046. NIPQUAD(saddr),
  1047. ntohs(uh->source),
  1048. ulen,
  1049. len,
  1050. NIPQUAD(daddr),
  1051. ntohs(uh->dest)));
  1052. no_header:
  1053. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1054. kfree_skb(skb);
  1055. return(0);
  1056. csum_error:
  1057. /*
  1058. * RFC1122: OK. Discards the bad packet silently (as far as
  1059. * the network is concerned, anyway) as per 4.1.3.4 (MUST).
  1060. */
  1061. LIMIT_NETDEBUG(printk(KERN_DEBUG "UDP: bad checksum. From %d.%d.%d.%d:%d to %d.%d.%d.%d:%d ulen %d\n",
  1062. NIPQUAD(saddr),
  1063. ntohs(uh->source),
  1064. NIPQUAD(daddr),
  1065. ntohs(uh->dest),
  1066. ulen));
  1067. drop:
  1068. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1069. kfree_skb(skb);
  1070. return(0);
  1071. }
  1072. static int udp_destroy_sock(struct sock *sk)
  1073. {
  1074. lock_sock(sk);
  1075. udp_flush_pending_frames(sk);
  1076. release_sock(sk);
  1077. return 0;
  1078. }
  1079. /*
  1080. * Socket option code for UDP
  1081. */
  1082. static int udp_setsockopt(struct sock *sk, int level, int optname,
  1083. char __user *optval, int optlen)
  1084. {
  1085. struct udp_sock *up = udp_sk(sk);
  1086. int val;
  1087. int err = 0;
  1088. if (level != SOL_UDP)
  1089. return ip_setsockopt(sk, level, optname, optval, optlen);
  1090. if(optlen<sizeof(int))
  1091. return -EINVAL;
  1092. if (get_user(val, (int __user *)optval))
  1093. return -EFAULT;
  1094. switch(optname) {
  1095. case UDP_CORK:
  1096. if (val != 0) {
  1097. up->corkflag = 1;
  1098. } else {
  1099. up->corkflag = 0;
  1100. lock_sock(sk);
  1101. udp_push_pending_frames(sk, up);
  1102. release_sock(sk);
  1103. }
  1104. break;
  1105. case UDP_ENCAP:
  1106. switch (val) {
  1107. case 0:
  1108. case UDP_ENCAP_ESPINUDP:
  1109. case UDP_ENCAP_ESPINUDP_NON_IKE:
  1110. up->encap_type = val;
  1111. break;
  1112. default:
  1113. err = -ENOPROTOOPT;
  1114. break;
  1115. }
  1116. break;
  1117. default:
  1118. err = -ENOPROTOOPT;
  1119. break;
  1120. };
  1121. return err;
  1122. }
  1123. static int udp_getsockopt(struct sock *sk, int level, int optname,
  1124. char __user *optval, int __user *optlen)
  1125. {
  1126. struct udp_sock *up = udp_sk(sk);
  1127. int val, len;
  1128. if (level != SOL_UDP)
  1129. return ip_getsockopt(sk, level, optname, optval, optlen);
  1130. if(get_user(len,optlen))
  1131. return -EFAULT;
  1132. len = min_t(unsigned int, len, sizeof(int));
  1133. if(len < 0)
  1134. return -EINVAL;
  1135. switch(optname) {
  1136. case UDP_CORK:
  1137. val = up->corkflag;
  1138. break;
  1139. case UDP_ENCAP:
  1140. val = up->encap_type;
  1141. break;
  1142. default:
  1143. return -ENOPROTOOPT;
  1144. };
  1145. if(put_user(len, optlen))
  1146. return -EFAULT;
  1147. if(copy_to_user(optval, &val,len))
  1148. return -EFAULT;
  1149. return 0;
  1150. }
  1151. /**
  1152. * udp_poll - wait for a UDP event.
  1153. * @file - file struct
  1154. * @sock - socket
  1155. * @wait - poll table
  1156. *
  1157. * This is same as datagram poll, except for the special case of
  1158. * blocking sockets. If application is using a blocking fd
  1159. * and a packet with checksum error is in the queue;
  1160. * then it could get return from select indicating data available
  1161. * but then block when reading it. Add special case code
  1162. * to work around these arguably broken applications.
  1163. */
  1164. unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
  1165. {
  1166. unsigned int mask = datagram_poll(file, sock, wait);
  1167. struct sock *sk = sock->sk;
  1168. /* Check for false positives due to checksum errors */
  1169. if ( (mask & POLLRDNORM) &&
  1170. !(file->f_flags & O_NONBLOCK) &&
  1171. !(sk->sk_shutdown & RCV_SHUTDOWN)){
  1172. struct sk_buff_head *rcvq = &sk->sk_receive_queue;
  1173. struct sk_buff *skb;
  1174. spin_lock_bh(&rcvq->lock);
  1175. while ((skb = skb_peek(rcvq)) != NULL) {
  1176. if (udp_checksum_complete(skb)) {
  1177. UDP_INC_STATS_BH(UDP_MIB_INERRORS);
  1178. __skb_unlink(skb, rcvq);
  1179. kfree_skb(skb);
  1180. } else {
  1181. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1182. break;
  1183. }
  1184. }
  1185. spin_unlock_bh(&rcvq->lock);
  1186. /* nothing to see, move along */
  1187. if (skb == NULL)
  1188. mask &= ~(POLLIN | POLLRDNORM);
  1189. }
  1190. return mask;
  1191. }
  1192. struct proto udp_prot = {
  1193. .name = "UDP",
  1194. .owner = THIS_MODULE,
  1195. .close = udp_close,
  1196. .connect = ip4_datagram_connect,
  1197. .disconnect = udp_disconnect,
  1198. .ioctl = udp_ioctl,
  1199. .destroy = udp_destroy_sock,
  1200. .setsockopt = udp_setsockopt,
  1201. .getsockopt = udp_getsockopt,
  1202. .sendmsg = udp_sendmsg,
  1203. .recvmsg = udp_recvmsg,
  1204. .sendpage = udp_sendpage,
  1205. .backlog_rcv = udp_queue_rcv_skb,
  1206. .hash = udp_v4_hash,
  1207. .unhash = udp_v4_unhash,
  1208. .get_port = udp_v4_get_port,
  1209. .obj_size = sizeof(struct udp_sock),
  1210. };
  1211. /* ------------------------------------------------------------------------ */
  1212. #ifdef CONFIG_PROC_FS
  1213. static struct sock *udp_get_first(struct seq_file *seq)
  1214. {
  1215. struct sock *sk;
  1216. struct udp_iter_state *state = seq->private;
  1217. for (state->bucket = 0; state->bucket < UDP_HTABLE_SIZE; ++state->bucket) {
  1218. struct hlist_node *node;
  1219. sk_for_each(sk, node, &udp_hash[state->bucket]) {
  1220. if (sk->sk_family == state->family)
  1221. goto found;
  1222. }
  1223. }
  1224. sk = NULL;
  1225. found:
  1226. return sk;
  1227. }
  1228. static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
  1229. {
  1230. struct udp_iter_state *state = seq->private;
  1231. do {
  1232. sk = sk_next(sk);
  1233. try_again:
  1234. ;
  1235. } while (sk && sk->sk_family != state->family);
  1236. if (!sk && ++state->bucket < UDP_HTABLE_SIZE) {
  1237. sk = sk_head(&udp_hash[state->bucket]);
  1238. goto try_again;
  1239. }
  1240. return sk;
  1241. }
  1242. static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
  1243. {
  1244. struct sock *sk = udp_get_first(seq);
  1245. if (sk)
  1246. while(pos && (sk = udp_get_next(seq, sk)) != NULL)
  1247. --pos;
  1248. return pos ? NULL : sk;
  1249. }
  1250. static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
  1251. {
  1252. read_lock(&udp_hash_lock);
  1253. return *pos ? udp_get_idx(seq, *pos-1) : (void *)1;
  1254. }
  1255. static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1256. {
  1257. struct sock *sk;
  1258. if (v == (void *)1)
  1259. sk = udp_get_idx(seq, 0);
  1260. else
  1261. sk = udp_get_next(seq, v);
  1262. ++*pos;
  1263. return sk;
  1264. }
  1265. static void udp_seq_stop(struct seq_file *seq, void *v)
  1266. {
  1267. read_unlock(&udp_hash_lock);
  1268. }
  1269. static int udp_seq_open(struct inode *inode, struct file *file)
  1270. {
  1271. struct udp_seq_afinfo *afinfo = PDE(inode)->data;
  1272. struct seq_file *seq;
  1273. int rc = -ENOMEM;
  1274. struct udp_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  1275. if (!s)
  1276. goto out;
  1277. memset(s, 0, sizeof(*s));
  1278. s->family = afinfo->family;
  1279. s->seq_ops.start = udp_seq_start;
  1280. s->seq_ops.next = udp_seq_next;
  1281. s->seq_ops.show = afinfo->seq_show;
  1282. s->seq_ops.stop = udp_seq_stop;
  1283. rc = seq_open(file, &s->seq_ops);
  1284. if (rc)
  1285. goto out_kfree;
  1286. seq = file->private_data;
  1287. seq->private = s;
  1288. out:
  1289. return rc;
  1290. out_kfree:
  1291. kfree(s);
  1292. goto out;
  1293. }
  1294. /* ------------------------------------------------------------------------ */
  1295. int udp_proc_register(struct udp_seq_afinfo *afinfo)
  1296. {
  1297. struct proc_dir_entry *p;
  1298. int rc = 0;
  1299. if (!afinfo)
  1300. return -EINVAL;
  1301. afinfo->seq_fops->owner = afinfo->owner;
  1302. afinfo->seq_fops->open = udp_seq_open;
  1303. afinfo->seq_fops->read = seq_read;
  1304. afinfo->seq_fops->llseek = seq_lseek;
  1305. afinfo->seq_fops->release = seq_release_private;
  1306. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1307. if (p)
  1308. p->data = afinfo;
  1309. else
  1310. rc = -ENOMEM;
  1311. return rc;
  1312. }
  1313. void udp_proc_unregister(struct udp_seq_afinfo *afinfo)
  1314. {
  1315. if (!afinfo)
  1316. return;
  1317. proc_net_remove(afinfo->name);
  1318. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1319. }
  1320. /* ------------------------------------------------------------------------ */
  1321. static void udp4_format_sock(struct sock *sp, char *tmpbuf, int bucket)
  1322. {
  1323. struct inet_sock *inet = inet_sk(sp);
  1324. unsigned int dest = inet->daddr;
  1325. unsigned int src = inet->rcv_saddr;
  1326. __u16 destp = ntohs(inet->dport);
  1327. __u16 srcp = ntohs(inet->sport);
  1328. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1329. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
  1330. bucket, src, srcp, dest, destp, sp->sk_state,
  1331. atomic_read(&sp->sk_wmem_alloc),
  1332. atomic_read(&sp->sk_rmem_alloc),
  1333. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  1334. atomic_read(&sp->sk_refcnt), sp);
  1335. }
  1336. static int udp4_seq_show(struct seq_file *seq, void *v)
  1337. {
  1338. if (v == SEQ_START_TOKEN)
  1339. seq_printf(seq, "%-127s\n",
  1340. " sl local_address rem_address st tx_queue "
  1341. "rx_queue tr tm->when retrnsmt uid timeout "
  1342. "inode");
  1343. else {
  1344. char tmpbuf[129];
  1345. struct udp_iter_state *state = seq->private;
  1346. udp4_format_sock(v, tmpbuf, state->bucket);
  1347. seq_printf(seq, "%-127s\n", tmpbuf);
  1348. }
  1349. return 0;
  1350. }
  1351. /* ------------------------------------------------------------------------ */
  1352. static struct file_operations udp4_seq_fops;
  1353. static struct udp_seq_afinfo udp4_seq_afinfo = {
  1354. .owner = THIS_MODULE,
  1355. .name = "udp",
  1356. .family = AF_INET,
  1357. .seq_show = udp4_seq_show,
  1358. .seq_fops = &udp4_seq_fops,
  1359. };
  1360. int __init udp4_proc_init(void)
  1361. {
  1362. return udp_proc_register(&udp4_seq_afinfo);
  1363. }
  1364. void udp4_proc_exit(void)
  1365. {
  1366. udp_proc_unregister(&udp4_seq_afinfo);
  1367. }
  1368. #endif /* CONFIG_PROC_FS */
  1369. EXPORT_SYMBOL(udp_disconnect);
  1370. EXPORT_SYMBOL(udp_hash);
  1371. EXPORT_SYMBOL(udp_hash_lock);
  1372. EXPORT_SYMBOL(udp_ioctl);
  1373. EXPORT_SYMBOL(udp_port_rover);
  1374. EXPORT_SYMBOL(udp_prot);
  1375. EXPORT_SYMBOL(udp_sendmsg);
  1376. EXPORT_SYMBOL(udp_poll);
  1377. #ifdef CONFIG_PROC_FS
  1378. EXPORT_SYMBOL(udp_proc_register);
  1379. EXPORT_SYMBOL(udp_proc_unregister);
  1380. #endif