ip_options.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 options processing module for ip.c
  7. *
  8. * Version: $Id: ip_options.c,v 1.21 2001/09/01 00:31:50 davem Exp $
  9. *
  10. * Authors: A.N.Kuznetsov
  11. *
  12. */
  13. #include <linux/capability.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <asm/uaccess.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/ip.h>
  19. #include <linux/icmp.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/rtnetlink.h>
  22. #include <net/sock.h>
  23. #include <net/ip.h>
  24. #include <net/icmp.h>
  25. #include <net/route.h>
  26. /*
  27. * Write options to IP header, record destination address to
  28. * source route option, address of outgoing interface
  29. * (we should already know it, so that this function is allowed be
  30. * called only after routing decision) and timestamp,
  31. * if we originate this datagram.
  32. *
  33. * daddr is real destination address, next hop is recorded in IP header.
  34. * saddr is address of outgoing interface.
  35. */
  36. void ip_options_build(struct sk_buff * skb, struct ip_options * opt,
  37. u32 daddr, struct rtable *rt, int is_frag)
  38. {
  39. unsigned char * iph = skb->nh.raw;
  40. memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
  41. memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
  42. opt = &(IPCB(skb)->opt);
  43. opt->is_data = 0;
  44. if (opt->srr)
  45. memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);
  46. if (!is_frag) {
  47. if (opt->rr_needaddr)
  48. ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, rt);
  49. if (opt->ts_needaddr)
  50. ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, rt);
  51. if (opt->ts_needtime) {
  52. struct timeval tv;
  53. __u32 midtime;
  54. do_gettimeofday(&tv);
  55. midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
  56. memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
  57. }
  58. return;
  59. }
  60. if (opt->rr) {
  61. memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]);
  62. opt->rr = 0;
  63. opt->rr_needaddr = 0;
  64. }
  65. if (opt->ts) {
  66. memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]);
  67. opt->ts = 0;
  68. opt->ts_needaddr = opt->ts_needtime = 0;
  69. }
  70. }
  71. /*
  72. * Provided (sopt, skb) points to received options,
  73. * build in dopt compiled option set appropriate for answering.
  74. * i.e. invert SRR option, copy anothers,
  75. * and grab room in RR/TS options.
  76. *
  77. * NOTE: dopt cannot point to skb.
  78. */
  79. int ip_options_echo(struct ip_options * dopt, struct sk_buff * skb)
  80. {
  81. struct ip_options *sopt;
  82. unsigned char *sptr, *dptr;
  83. int soffset, doffset;
  84. int optlen;
  85. u32 daddr;
  86. memset(dopt, 0, sizeof(struct ip_options));
  87. dopt->is_data = 1;
  88. sopt = &(IPCB(skb)->opt);
  89. if (sopt->optlen == 0) {
  90. dopt->optlen = 0;
  91. return 0;
  92. }
  93. sptr = skb->nh.raw;
  94. dptr = dopt->__data;
  95. if (skb->dst)
  96. daddr = ((struct rtable*)skb->dst)->rt_spec_dst;
  97. else
  98. daddr = skb->nh.iph->daddr;
  99. if (sopt->rr) {
  100. optlen = sptr[sopt->rr+1];
  101. soffset = sptr[sopt->rr+2];
  102. dopt->rr = dopt->optlen + sizeof(struct iphdr);
  103. memcpy(dptr, sptr+sopt->rr, optlen);
  104. if (sopt->rr_needaddr && soffset <= optlen) {
  105. if (soffset + 3 > optlen)
  106. return -EINVAL;
  107. dptr[2] = soffset + 4;
  108. dopt->rr_needaddr = 1;
  109. }
  110. dptr += optlen;
  111. dopt->optlen += optlen;
  112. }
  113. if (sopt->ts) {
  114. optlen = sptr[sopt->ts+1];
  115. soffset = sptr[sopt->ts+2];
  116. dopt->ts = dopt->optlen + sizeof(struct iphdr);
  117. memcpy(dptr, sptr+sopt->ts, optlen);
  118. if (soffset <= optlen) {
  119. if (sopt->ts_needaddr) {
  120. if (soffset + 3 > optlen)
  121. return -EINVAL;
  122. dopt->ts_needaddr = 1;
  123. soffset += 4;
  124. }
  125. if (sopt->ts_needtime) {
  126. if (soffset + 3 > optlen)
  127. return -EINVAL;
  128. if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
  129. dopt->ts_needtime = 1;
  130. soffset += 4;
  131. } else {
  132. dopt->ts_needtime = 0;
  133. if (soffset + 8 <= optlen) {
  134. __u32 addr;
  135. memcpy(&addr, sptr+soffset-1, 4);
  136. if (inet_addr_type(addr) != RTN_LOCAL) {
  137. dopt->ts_needtime = 1;
  138. soffset += 8;
  139. }
  140. }
  141. }
  142. }
  143. dptr[2] = soffset;
  144. }
  145. dptr += optlen;
  146. dopt->optlen += optlen;
  147. }
  148. if (sopt->srr) {
  149. unsigned char * start = sptr+sopt->srr;
  150. u32 faddr;
  151. optlen = start[1];
  152. soffset = start[2];
  153. doffset = 0;
  154. if (soffset > optlen)
  155. soffset = optlen + 1;
  156. soffset -= 4;
  157. if (soffset > 3) {
  158. memcpy(&faddr, &start[soffset-1], 4);
  159. for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4)
  160. memcpy(&dptr[doffset-1], &start[soffset-1], 4);
  161. /*
  162. * RFC1812 requires to fix illegal source routes.
  163. */
  164. if (memcmp(&skb->nh.iph->saddr, &start[soffset+3], 4) == 0)
  165. doffset -= 4;
  166. }
  167. if (doffset > 3) {
  168. memcpy(&start[doffset-1], &daddr, 4);
  169. dopt->faddr = faddr;
  170. dptr[0] = start[0];
  171. dptr[1] = doffset+3;
  172. dptr[2] = 4;
  173. dptr += doffset+3;
  174. dopt->srr = dopt->optlen + sizeof(struct iphdr);
  175. dopt->optlen += doffset+3;
  176. dopt->is_strictroute = sopt->is_strictroute;
  177. }
  178. }
  179. while (dopt->optlen & 3) {
  180. *dptr++ = IPOPT_END;
  181. dopt->optlen++;
  182. }
  183. return 0;
  184. }
  185. /*
  186. * Options "fragmenting", just fill options not
  187. * allowed in fragments with NOOPs.
  188. * Simple and stupid 8), but the most efficient way.
  189. */
  190. void ip_options_fragment(struct sk_buff * skb)
  191. {
  192. unsigned char * optptr = skb->nh.raw + sizeof(struct iphdr);
  193. struct ip_options * opt = &(IPCB(skb)->opt);
  194. int l = opt->optlen;
  195. int optlen;
  196. while (l > 0) {
  197. switch (*optptr) {
  198. case IPOPT_END:
  199. return;
  200. case IPOPT_NOOP:
  201. l--;
  202. optptr++;
  203. continue;
  204. }
  205. optlen = optptr[1];
  206. if (optlen<2 || optlen>l)
  207. return;
  208. if (!IPOPT_COPIED(*optptr))
  209. memset(optptr, IPOPT_NOOP, optlen);
  210. l -= optlen;
  211. optptr += optlen;
  212. }
  213. opt->ts = 0;
  214. opt->rr = 0;
  215. opt->rr_needaddr = 0;
  216. opt->ts_needaddr = 0;
  217. opt->ts_needtime = 0;
  218. return;
  219. }
  220. /*
  221. * Verify options and fill pointers in struct options.
  222. * Caller should clear *opt, and set opt->data.
  223. * If opt == NULL, then skb->data should point to IP header.
  224. */
  225. int ip_options_compile(struct ip_options * opt, struct sk_buff * skb)
  226. {
  227. int l;
  228. unsigned char * iph;
  229. unsigned char * optptr;
  230. int optlen;
  231. unsigned char * pp_ptr = NULL;
  232. struct rtable *rt = skb ? (struct rtable*)skb->dst : NULL;
  233. if (!opt) {
  234. opt = &(IPCB(skb)->opt);
  235. iph = skb->nh.raw;
  236. opt->optlen = ((struct iphdr *)iph)->ihl*4 - sizeof(struct iphdr);
  237. optptr = iph + sizeof(struct iphdr);
  238. opt->is_data = 0;
  239. } else {
  240. optptr = opt->is_data ? opt->__data : (unsigned char*)&(skb->nh.iph[1]);
  241. iph = optptr - sizeof(struct iphdr);
  242. }
  243. for (l = opt->optlen; l > 0; ) {
  244. switch (*optptr) {
  245. case IPOPT_END:
  246. for (optptr++, l--; l>0; optptr++, l--) {
  247. if (*optptr != IPOPT_END) {
  248. *optptr = IPOPT_END;
  249. opt->is_changed = 1;
  250. }
  251. }
  252. goto eol;
  253. case IPOPT_NOOP:
  254. l--;
  255. optptr++;
  256. continue;
  257. }
  258. optlen = optptr[1];
  259. if (optlen<2 || optlen>l) {
  260. pp_ptr = optptr;
  261. goto error;
  262. }
  263. switch (*optptr) {
  264. case IPOPT_SSRR:
  265. case IPOPT_LSRR:
  266. if (optlen < 3) {
  267. pp_ptr = optptr + 1;
  268. goto error;
  269. }
  270. if (optptr[2] < 4) {
  271. pp_ptr = optptr + 2;
  272. goto error;
  273. }
  274. /* NB: cf RFC-1812 5.2.4.1 */
  275. if (opt->srr) {
  276. pp_ptr = optptr;
  277. goto error;
  278. }
  279. if (!skb) {
  280. if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
  281. pp_ptr = optptr + 1;
  282. goto error;
  283. }
  284. memcpy(&opt->faddr, &optptr[3], 4);
  285. if (optlen > 7)
  286. memmove(&optptr[3], &optptr[7], optlen-7);
  287. }
  288. opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
  289. opt->srr = optptr - iph;
  290. break;
  291. case IPOPT_RR:
  292. if (opt->rr) {
  293. pp_ptr = optptr;
  294. goto error;
  295. }
  296. if (optlen < 3) {
  297. pp_ptr = optptr + 1;
  298. goto error;
  299. }
  300. if (optptr[2] < 4) {
  301. pp_ptr = optptr + 2;
  302. goto error;
  303. }
  304. if (optptr[2] <= optlen) {
  305. if (optptr[2]+3 > optlen) {
  306. pp_ptr = optptr + 2;
  307. goto error;
  308. }
  309. if (skb) {
  310. memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
  311. opt->is_changed = 1;
  312. }
  313. optptr[2] += 4;
  314. opt->rr_needaddr = 1;
  315. }
  316. opt->rr = optptr - iph;
  317. break;
  318. case IPOPT_TIMESTAMP:
  319. if (opt->ts) {
  320. pp_ptr = optptr;
  321. goto error;
  322. }
  323. if (optlen < 4) {
  324. pp_ptr = optptr + 1;
  325. goto error;
  326. }
  327. if (optptr[2] < 5) {
  328. pp_ptr = optptr + 2;
  329. goto error;
  330. }
  331. if (optptr[2] <= optlen) {
  332. __u32 * timeptr = NULL;
  333. if (optptr[2]+3 > optptr[1]) {
  334. pp_ptr = optptr + 2;
  335. goto error;
  336. }
  337. switch (optptr[3]&0xF) {
  338. case IPOPT_TS_TSONLY:
  339. opt->ts = optptr - iph;
  340. if (skb)
  341. timeptr = (__u32*)&optptr[optptr[2]-1];
  342. opt->ts_needtime = 1;
  343. optptr[2] += 4;
  344. break;
  345. case IPOPT_TS_TSANDADDR:
  346. if (optptr[2]+7 > optptr[1]) {
  347. pp_ptr = optptr + 2;
  348. goto error;
  349. }
  350. opt->ts = optptr - iph;
  351. if (skb) {
  352. memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
  353. timeptr = (__u32*)&optptr[optptr[2]+3];
  354. }
  355. opt->ts_needaddr = 1;
  356. opt->ts_needtime = 1;
  357. optptr[2] += 8;
  358. break;
  359. case IPOPT_TS_PRESPEC:
  360. if (optptr[2]+7 > optptr[1]) {
  361. pp_ptr = optptr + 2;
  362. goto error;
  363. }
  364. opt->ts = optptr - iph;
  365. {
  366. u32 addr;
  367. memcpy(&addr, &optptr[optptr[2]-1], 4);
  368. if (inet_addr_type(addr) == RTN_UNICAST)
  369. break;
  370. if (skb)
  371. timeptr = (__u32*)&optptr[optptr[2]+3];
  372. }
  373. opt->ts_needtime = 1;
  374. optptr[2] += 8;
  375. break;
  376. default:
  377. if (!skb && !capable(CAP_NET_RAW)) {
  378. pp_ptr = optptr + 3;
  379. goto error;
  380. }
  381. break;
  382. }
  383. if (timeptr) {
  384. struct timeval tv;
  385. __u32 midtime;
  386. do_gettimeofday(&tv);
  387. midtime = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
  388. memcpy(timeptr, &midtime, sizeof(__u32));
  389. opt->is_changed = 1;
  390. }
  391. } else {
  392. unsigned overflow = optptr[3]>>4;
  393. if (overflow == 15) {
  394. pp_ptr = optptr + 3;
  395. goto error;
  396. }
  397. opt->ts = optptr - iph;
  398. if (skb) {
  399. optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
  400. opt->is_changed = 1;
  401. }
  402. }
  403. break;
  404. case IPOPT_RA:
  405. if (optlen < 4) {
  406. pp_ptr = optptr + 1;
  407. goto error;
  408. }
  409. if (optptr[2] == 0 && optptr[3] == 0)
  410. opt->router_alert = optptr - iph;
  411. break;
  412. case IPOPT_SEC:
  413. case IPOPT_SID:
  414. default:
  415. if (!skb && !capable(CAP_NET_RAW)) {
  416. pp_ptr = optptr;
  417. goto error;
  418. }
  419. break;
  420. }
  421. l -= optlen;
  422. optptr += optlen;
  423. }
  424. eol:
  425. if (!pp_ptr)
  426. return 0;
  427. error:
  428. if (skb) {
  429. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
  430. }
  431. return -EINVAL;
  432. }
  433. /*
  434. * Undo all the changes done by ip_options_compile().
  435. */
  436. void ip_options_undo(struct ip_options * opt)
  437. {
  438. if (opt->srr) {
  439. unsigned char * optptr = opt->__data+opt->srr-sizeof(struct iphdr);
  440. memmove(optptr+7, optptr+3, optptr[1]-7);
  441. memcpy(optptr+3, &opt->faddr, 4);
  442. }
  443. if (opt->rr_needaddr) {
  444. unsigned char * optptr = opt->__data+opt->rr-sizeof(struct iphdr);
  445. optptr[2] -= 4;
  446. memset(&optptr[optptr[2]-1], 0, 4);
  447. }
  448. if (opt->ts) {
  449. unsigned char * optptr = opt->__data+opt->ts-sizeof(struct iphdr);
  450. if (opt->ts_needtime) {
  451. optptr[2] -= 4;
  452. memset(&optptr[optptr[2]-1], 0, 4);
  453. if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
  454. optptr[2] -= 4;
  455. }
  456. if (opt->ts_needaddr) {
  457. optptr[2] -= 4;
  458. memset(&optptr[optptr[2]-1], 0, 4);
  459. }
  460. }
  461. }
  462. static struct ip_options *ip_options_get_alloc(const int optlen)
  463. {
  464. struct ip_options *opt = kmalloc(sizeof(*opt) + ((optlen + 3) & ~3),
  465. GFP_KERNEL);
  466. if (opt)
  467. memset(opt, 0, sizeof(*opt));
  468. return opt;
  469. }
  470. static int ip_options_get_finish(struct ip_options **optp,
  471. struct ip_options *opt, int optlen)
  472. {
  473. while (optlen & 3)
  474. opt->__data[optlen++] = IPOPT_END;
  475. opt->optlen = optlen;
  476. opt->is_data = 1;
  477. opt->is_setbyuser = 1;
  478. if (optlen && ip_options_compile(opt, NULL)) {
  479. kfree(opt);
  480. return -EINVAL;
  481. }
  482. kfree(*optp);
  483. *optp = opt;
  484. return 0;
  485. }
  486. int ip_options_get_from_user(struct ip_options **optp, unsigned char __user *data, int optlen)
  487. {
  488. struct ip_options *opt = ip_options_get_alloc(optlen);
  489. if (!opt)
  490. return -ENOMEM;
  491. if (optlen && copy_from_user(opt->__data, data, optlen)) {
  492. kfree(opt);
  493. return -EFAULT;
  494. }
  495. return ip_options_get_finish(optp, opt, optlen);
  496. }
  497. int ip_options_get(struct ip_options **optp, unsigned char *data, int optlen)
  498. {
  499. struct ip_options *opt = ip_options_get_alloc(optlen);
  500. if (!opt)
  501. return -ENOMEM;
  502. if (optlen)
  503. memcpy(opt->__data, data, optlen);
  504. return ip_options_get_finish(optp, opt, optlen);
  505. }
  506. void ip_forward_options(struct sk_buff *skb)
  507. {
  508. struct ip_options * opt = &(IPCB(skb)->opt);
  509. unsigned char * optptr;
  510. struct rtable *rt = (struct rtable*)skb->dst;
  511. unsigned char *raw = skb->nh.raw;
  512. if (opt->rr_needaddr) {
  513. optptr = (unsigned char *)raw + opt->rr;
  514. ip_rt_get_source(&optptr[optptr[2]-5], rt);
  515. opt->is_changed = 1;
  516. }
  517. if (opt->srr_is_hit) {
  518. int srrptr, srrspace;
  519. optptr = raw + opt->srr;
  520. for ( srrptr=optptr[2], srrspace = optptr[1];
  521. srrptr <= srrspace;
  522. srrptr += 4
  523. ) {
  524. if (srrptr + 3 > srrspace)
  525. break;
  526. if (memcmp(&rt->rt_dst, &optptr[srrptr-1], 4) == 0)
  527. break;
  528. }
  529. if (srrptr + 3 <= srrspace) {
  530. opt->is_changed = 1;
  531. ip_rt_get_source(&optptr[srrptr-1], rt);
  532. skb->nh.iph->daddr = rt->rt_dst;
  533. optptr[2] = srrptr+4;
  534. } else if (net_ratelimit())
  535. printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
  536. if (opt->ts_needaddr) {
  537. optptr = raw + opt->ts;
  538. ip_rt_get_source(&optptr[optptr[2]-9], rt);
  539. opt->is_changed = 1;
  540. }
  541. }
  542. if (opt->is_changed) {
  543. opt->is_changed = 0;
  544. ip_send_check(skb->nh.iph);
  545. }
  546. }
  547. int ip_options_rcv_srr(struct sk_buff *skb)
  548. {
  549. struct ip_options *opt = &(IPCB(skb)->opt);
  550. int srrspace, srrptr;
  551. u32 nexthop;
  552. struct iphdr *iph = skb->nh.iph;
  553. unsigned char * optptr = skb->nh.raw + opt->srr;
  554. struct rtable *rt = (struct rtable*)skb->dst;
  555. struct rtable *rt2;
  556. int err;
  557. if (!opt->srr)
  558. return 0;
  559. if (skb->pkt_type != PACKET_HOST)
  560. return -EINVAL;
  561. if (rt->rt_type == RTN_UNICAST) {
  562. if (!opt->is_strictroute)
  563. return 0;
  564. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
  565. return -EINVAL;
  566. }
  567. if (rt->rt_type != RTN_LOCAL)
  568. return -EINVAL;
  569. for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
  570. if (srrptr + 3 > srrspace) {
  571. icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
  572. return -EINVAL;
  573. }
  574. memcpy(&nexthop, &optptr[srrptr-1], 4);
  575. rt = (struct rtable*)skb->dst;
  576. skb->dst = NULL;
  577. err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
  578. rt2 = (struct rtable*)skb->dst;
  579. if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
  580. ip_rt_put(rt2);
  581. skb->dst = &rt->u.dst;
  582. return -EINVAL;
  583. }
  584. ip_rt_put(rt);
  585. if (rt2->rt_type != RTN_LOCAL)
  586. break;
  587. /* Superfast 8) loopback forward */
  588. memcpy(&iph->daddr, &optptr[srrptr-1], 4);
  589. opt->is_changed = 1;
  590. }
  591. if (srrptr <= srrspace) {
  592. opt->srr_is_hit = 1;
  593. opt->is_changed = 1;
  594. }
  595. return 0;
  596. }