blob: ea80450efe564a3ea36049ef83d9e0b6222eb9fe [file] [log] [blame]
Huw Daviescb72d382016-06-27 15:02:46 -04001/*
2 * CALIPSO - Common Architecture Label IPv6 Security Option
3 *
4 * This is an implementation of the CALIPSO protocol as specified in
5 * RFC 5570.
6 *
7 * Authors: Paul Moore <paul.moore@hp.com>
8 * Huw Davies <huw@codeweavers.com>
9 *
10 */
11
12/* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
13 * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30#include <linux/init.h>
31#include <linux/types.h>
32#include <linux/rcupdate.h>
33#include <linux/list.h>
34#include <linux/spinlock.h>
35#include <linux/string.h>
36#include <linux/jhash.h>
37#include <linux/audit.h>
38#include <linux/slab.h>
39#include <net/ip.h>
40#include <net/icmp.h>
41#include <net/tcp.h>
42#include <net/netlabel.h>
43#include <net/calipso.h>
44#include <linux/atomic.h>
45#include <linux/bug.h>
46#include <asm/unaligned.h>
Huw Daviesceba1832016-06-27 15:02:51 -040047#include <linux/crc-ccitt.h>
48
49/* Maximium size of the calipso option including
50 * the two-byte TLV header.
51 */
52#define CALIPSO_OPT_LEN_MAX (2 + 252)
53
54/* Size of the minimum calipso option including
55 * the two-byte TLV header.
56 */
57#define CALIPSO_HDR_LEN (2 + 8)
58
59/* Maximium size of the calipso option including
60 * the two-byte TLV header and upto 3 bytes of
61 * leading pad and 7 bytes of trailing pad.
62 */
63#define CALIPSO_OPT_LEN_MAX_WITH_PAD (3 + CALIPSO_OPT_LEN_MAX + 7)
64
Huw Davies2917f572016-06-27 15:06:15 -040065 /* Maximium size of u32 aligned buffer required to hold calipso
66 * option. Max of 3 initial pad bytes starting from buffer + 3.
67 * i.e. the worst case is when the previous tlv finishes on 4n + 3.
68 */
69#define CALIPSO_MAX_BUFFER (6 + CALIPSO_OPT_LEN_MAX)
Huw Daviescb72d382016-06-27 15:02:46 -040070
71/* List of available DOI definitions */
72static DEFINE_SPINLOCK(calipso_doi_list_lock);
73static LIST_HEAD(calipso_doi_list);
74
75/* DOI List Functions
76 */
77
78/**
79 * calipso_doi_search - Searches for a DOI definition
80 * @doi: the DOI to search for
81 *
82 * Description:
83 * Search the DOI definition list for a DOI definition with a DOI value that
84 * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
85 * Returns a pointer to the DOI definition on success and NULL on failure.
86 */
87static struct calipso_doi *calipso_doi_search(u32 doi)
88{
89 struct calipso_doi *iter;
90
91 list_for_each_entry_rcu(iter, &calipso_doi_list, list)
92 if (iter->doi == doi && atomic_read(&iter->refcount))
93 return iter;
94 return NULL;
95}
96
97/**
98 * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
99 * @doi_def: the DOI structure
100 * @audit_info: NetLabel audit information
101 *
102 * Description:
103 * The caller defines a new DOI for use by the CALIPSO engine and calls this
104 * function to add it to the list of acceptable domains. The caller must
105 * ensure that the mapping table specified in @doi_def->map meets all of the
106 * requirements of the mapping type (see calipso.h for details). Returns
107 * zero on success and non-zero on failure.
108 *
109 */
110static int calipso_doi_add(struct calipso_doi *doi_def,
111 struct netlbl_audit *audit_info)
112{
113 int ret_val = -EINVAL;
114 u32 doi;
115 u32 doi_type;
116 struct audit_buffer *audit_buf;
117
118 doi = doi_def->doi;
119 doi_type = doi_def->type;
120
121 if (doi_def->doi == CALIPSO_DOI_UNKNOWN)
122 goto doi_add_return;
123
124 atomic_set(&doi_def->refcount, 1);
125
126 spin_lock(&calipso_doi_list_lock);
127 if (calipso_doi_search(doi_def->doi)) {
128 spin_unlock(&calipso_doi_list_lock);
129 ret_val = -EEXIST;
130 goto doi_add_return;
131 }
132 list_add_tail_rcu(&doi_def->list, &calipso_doi_list);
133 spin_unlock(&calipso_doi_list_lock);
134 ret_val = 0;
135
136doi_add_return:
137 audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_ADD, audit_info);
138 if (audit_buf) {
139 const char *type_str;
140
141 switch (doi_type) {
142 case CALIPSO_MAP_PASS:
143 type_str = "pass";
144 break;
145 default:
146 type_str = "(unknown)";
147 }
148 audit_log_format(audit_buf,
149 " calipso_doi=%u calipso_type=%s res=%u",
150 doi, type_str, ret_val == 0 ? 1 : 0);
151 audit_log_end(audit_buf);
152 }
153
154 return ret_val;
155}
156
157/**
158 * calipso_doi_free - Frees a DOI definition
159 * @doi_def: the DOI definition
160 *
161 * Description:
162 * This function frees all of the memory associated with a DOI definition.
163 *
164 */
165static void calipso_doi_free(struct calipso_doi *doi_def)
166{
167 kfree(doi_def);
168}
169
Huw Daviesa5e34492016-06-27 15:02:47 -0400170/**
171 * calipso_doi_free_rcu - Frees a DOI definition via the RCU pointer
172 * @entry: the entry's RCU field
173 *
174 * Description:
175 * This function is designed to be used as a callback to the call_rcu()
176 * function so that the memory allocated to the DOI definition can be released
177 * safely.
178 *
179 */
180static void calipso_doi_free_rcu(struct rcu_head *entry)
181{
182 struct calipso_doi *doi_def;
183
184 doi_def = container_of(entry, struct calipso_doi, rcu);
185 calipso_doi_free(doi_def);
186}
187
188/**
Huw Daviesd7cce012016-06-27 15:02:49 -0400189 * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
190 * @doi: the DOI value
191 * @audit_secid: the LSM secid to use in the audit message
192 *
193 * Description:
194 * Removes a DOI definition from the CALIPSO engine. The NetLabel routines will
195 * be called to release their own LSM domain mappings as well as our own
196 * domain list. Returns zero on success and negative values on failure.
197 *
198 */
199static int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
200{
201 int ret_val;
202 struct calipso_doi *doi_def;
203 struct audit_buffer *audit_buf;
204
205 spin_lock(&calipso_doi_list_lock);
206 doi_def = calipso_doi_search(doi);
207 if (!doi_def) {
208 spin_unlock(&calipso_doi_list_lock);
209 ret_val = -ENOENT;
210 goto doi_remove_return;
211 }
212 if (!atomic_dec_and_test(&doi_def->refcount)) {
213 spin_unlock(&calipso_doi_list_lock);
214 ret_val = -EBUSY;
215 goto doi_remove_return;
216 }
217 list_del_rcu(&doi_def->list);
218 spin_unlock(&calipso_doi_list_lock);
219
220 call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
221 ret_val = 0;
222
223doi_remove_return:
224 audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_DEL, audit_info);
225 if (audit_buf) {
226 audit_log_format(audit_buf,
227 " calipso_doi=%u res=%u",
228 doi, ret_val == 0 ? 1 : 0);
229 audit_log_end(audit_buf);
230 }
231
232 return ret_val;
233}
234
235/**
Huw Daviesa5e34492016-06-27 15:02:47 -0400236 * calipso_doi_getdef - Returns a reference to a valid DOI definition
237 * @doi: the DOI value
238 *
239 * Description:
240 * Searches for a valid DOI definition and if one is found it is returned to
241 * the caller. Otherwise NULL is returned. The caller must ensure that
242 * calipso_doi_putdef() is called when the caller is done.
243 *
244 */
245static struct calipso_doi *calipso_doi_getdef(u32 doi)
246{
247 struct calipso_doi *doi_def;
248
249 rcu_read_lock();
250 doi_def = calipso_doi_search(doi);
251 if (!doi_def)
252 goto doi_getdef_return;
253 if (!atomic_inc_not_zero(&doi_def->refcount))
254 doi_def = NULL;
255
256doi_getdef_return:
257 rcu_read_unlock();
258 return doi_def;
259}
260
261/**
262 * calipso_doi_putdef - Releases a reference for the given DOI definition
263 * @doi_def: the DOI definition
264 *
265 * Description:
266 * Releases a DOI definition reference obtained from calipso_doi_getdef().
267 *
268 */
269static void calipso_doi_putdef(struct calipso_doi *doi_def)
270{
271 if (!doi_def)
272 return;
273
274 if (!atomic_dec_and_test(&doi_def->refcount))
275 return;
276 spin_lock(&calipso_doi_list_lock);
277 list_del_rcu(&doi_def->list);
278 spin_unlock(&calipso_doi_list_lock);
279
280 call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
281}
282
Huw Daviese1ce69d2016-06-27 15:02:48 -0400283/**
284 * calipso_doi_walk - Iterate through the DOI definitions
285 * @skip_cnt: skip past this number of DOI definitions, updated
286 * @callback: callback for each DOI definition
287 * @cb_arg: argument for the callback function
288 *
289 * Description:
290 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
291 * For each entry call @callback, if @callback returns a negative value stop
292 * 'walking' through the list and return. Updates the value in @skip_cnt upon
293 * return. Returns zero on success, negative values on failure.
294 *
295 */
296static int calipso_doi_walk(u32 *skip_cnt,
297 int (*callback)(struct calipso_doi *doi_def,
298 void *arg),
299 void *cb_arg)
300{
301 int ret_val = -ENOENT;
302 u32 doi_cnt = 0;
303 struct calipso_doi *iter_doi;
304
305 rcu_read_lock();
306 list_for_each_entry_rcu(iter_doi, &calipso_doi_list, list)
307 if (atomic_read(&iter_doi->refcount) > 0) {
308 if (doi_cnt++ < *skip_cnt)
309 continue;
310 ret_val = callback(iter_doi, cb_arg);
311 if (ret_val < 0) {
312 doi_cnt--;
313 goto doi_walk_return;
314 }
315 }
316
317doi_walk_return:
318 rcu_read_unlock();
319 *skip_cnt = doi_cnt;
320 return ret_val;
321}
322
Huw Daviesceba1832016-06-27 15:02:51 -0400323/**
Huw Davies2e532b72016-06-27 15:06:17 -0400324 * calipso_validate - Validate a CALIPSO option
325 * @skb: the packet
326 * @option: the start of the option
327 *
328 * Description:
329 * This routine is called to validate a CALIPSO option.
330 * If the option is valid then %true is returned, otherwise
331 * %false is returned.
332 *
333 * The caller should have already checked that the length of the
334 * option (including the TLV header) is >= 10 and that the catmap
335 * length is consistent with the option length.
336 *
337 * We leave checks on the level and categories to the socket layer.
338 */
339bool calipso_validate(const struct sk_buff *skb, const unsigned char *option)
340{
341 struct calipso_doi *doi_def;
342 bool ret_val;
343 u16 crc, len = option[1] + 2;
344 static const u8 zero[2];
345
346 /* The original CRC runs over the option including the TLV header
347 * with the CRC-16 field (at offset 8) zeroed out. */
348 crc = crc_ccitt(0xffff, option, 8);
349 crc = crc_ccitt(crc, zero, sizeof(zero));
350 if (len > 10)
351 crc = crc_ccitt(crc, option + 10, len - 10);
352 crc = ~crc;
353 if (option[8] != (crc & 0xff) || option[9] != ((crc >> 8) & 0xff))
354 return false;
355
356 rcu_read_lock();
357 doi_def = calipso_doi_search(get_unaligned_be32(option + 2));
358 ret_val = !!doi_def;
359 rcu_read_unlock();
360
361 return ret_val;
362}
363
364/**
Huw Daviesceba1832016-06-27 15:02:51 -0400365 * calipso_map_cat_hton - Perform a category mapping from host to network
366 * @doi_def: the DOI definition
367 * @secattr: the security attributes
368 * @net_cat: the zero'd out category bitmap in network/CALIPSO format
369 * @net_cat_len: the length of the CALIPSO bitmap in bytes
370 *
371 * Description:
372 * Perform a label mapping to translate a local MLS category bitmap to the
373 * correct CALIPSO bitmap using the given DOI definition. Returns the minimum
374 * size in bytes of the network bitmap on success, negative values otherwise.
375 *
376 */
377static int calipso_map_cat_hton(const struct calipso_doi *doi_def,
378 const struct netlbl_lsm_secattr *secattr,
379 unsigned char *net_cat,
380 u32 net_cat_len)
381{
382 int spot = -1;
383 u32 net_spot_max = 0;
384 u32 net_clen_bits = net_cat_len * 8;
385
386 for (;;) {
387 spot = netlbl_catmap_walk(secattr->attr.mls.cat,
388 spot + 1);
389 if (spot < 0)
390 break;
391 if (spot >= net_clen_bits)
392 return -ENOSPC;
393 netlbl_bitmap_setbit(net_cat, spot, 1);
394
395 if (spot > net_spot_max)
396 net_spot_max = spot;
397 }
398
399 return (net_spot_max / 32 + 1) * 4;
400}
401
402/**
403 * calipso_map_cat_ntoh - Perform a category mapping from network to host
404 * @doi_def: the DOI definition
405 * @net_cat: the category bitmap in network/CALIPSO format
406 * @net_cat_len: the length of the CALIPSO bitmap in bytes
407 * @secattr: the security attributes
408 *
409 * Description:
410 * Perform a label mapping to translate a CALIPSO bitmap to the correct local
411 * MLS category bitmap using the given DOI definition. Returns zero on
412 * success, negative values on failure.
413 *
414 */
415static int calipso_map_cat_ntoh(const struct calipso_doi *doi_def,
416 const unsigned char *net_cat,
417 u32 net_cat_len,
418 struct netlbl_lsm_secattr *secattr)
419{
420 int ret_val;
421 int spot = -1;
422 u32 net_clen_bits = net_cat_len * 8;
423
424 for (;;) {
425 spot = netlbl_bitmap_walk(net_cat,
426 net_clen_bits,
427 spot + 1,
428 1);
429 if (spot < 0) {
430 if (spot == -2)
431 return -EFAULT;
432 return 0;
433 }
434
435 ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
436 spot,
437 GFP_ATOMIC);
438 if (ret_val != 0)
439 return ret_val;
440 }
441
442 return -EINVAL;
443}
444
445/**
446 * calipso_pad_write - Writes pad bytes in TLV format
447 * @buf: the buffer
448 * @offset: offset from start of buffer to write padding
449 * @count: number of pad bytes to write
450 *
451 * Description:
452 * Write @count bytes of TLV padding into @buffer starting at offset @offset.
453 * @count should be less than 8 - see RFC 4942.
454 *
455 */
456static int calipso_pad_write(unsigned char *buf, unsigned int offset,
457 unsigned int count)
458{
459 if (WARN_ON_ONCE(count >= 8))
460 return -EINVAL;
461
462 switch (count) {
463 case 0:
464 break;
465 case 1:
466 buf[offset] = IPV6_TLV_PAD1;
467 break;
468 default:
469 buf[offset] = IPV6_TLV_PADN;
470 buf[offset + 1] = count - 2;
471 if (count > 2)
472 memset(buf + offset + 2, 0, count - 2);
473 break;
474 }
475 return 0;
476}
477
478/**
479 * calipso_genopt - Generate a CALIPSO option
480 * @buf: the option buffer
481 * @start: offset from which to write
482 * @buf_len: the size of opt_buf
483 * @doi_def: the CALIPSO DOI to use
484 * @secattr: the security attributes
485 *
486 * Description:
487 * Generate a CALIPSO option using the DOI definition and security attributes
488 * passed to the function. This also generates upto three bytes of leading
489 * padding that ensures that the option is 4n + 2 aligned. It returns the
490 * number of bytes written (including any initial padding).
491 */
492static int calipso_genopt(unsigned char *buf, u32 start, u32 buf_len,
493 const struct calipso_doi *doi_def,
494 const struct netlbl_lsm_secattr *secattr)
495{
496 int ret_val;
497 u32 len, pad;
498 u16 crc;
499 static const unsigned char padding[4] = {2, 1, 0, 3};
500 unsigned char *calipso;
501
502 /* CALIPSO has 4n + 2 alignment */
503 pad = padding[start & 3];
504 if (buf_len <= start + pad + CALIPSO_HDR_LEN)
505 return -ENOSPC;
506
507 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
508 return -EPERM;
509
510 len = CALIPSO_HDR_LEN;
511
512 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
513 ret_val = calipso_map_cat_hton(doi_def,
514 secattr,
515 buf + start + pad + len,
516 buf_len - start - pad - len);
517 if (ret_val < 0)
518 return ret_val;
519 len += ret_val;
520 }
521
522 calipso_pad_write(buf, start, pad);
523 calipso = buf + start + pad;
524
525 calipso[0] = IPV6_TLV_CALIPSO;
526 calipso[1] = len - 2;
527 *(__be32 *)(calipso + 2) = htonl(doi_def->doi);
528 calipso[6] = (len - CALIPSO_HDR_LEN) / 4;
529 calipso[7] = secattr->attr.mls.lvl,
530 crc = ~crc_ccitt(0xffff, calipso, len);
531 calipso[8] = crc & 0xff;
532 calipso[9] = (crc >> 8) & 0xff;
533 return pad + len;
534}
535
536/* Hop-by-hop hdr helper functions
537 */
538
539/**
540 * calipso_opt_update - Replaces socket's hop options with a new set
541 * @sk: the socket
542 * @hop: new hop options
543 *
544 * Description:
545 * Replaces @sk's hop options with @hop. @hop may be NULL to leave
546 * the socket with no hop options.
547 *
548 */
549static int calipso_opt_update(struct sock *sk, struct ipv6_opt_hdr *hop)
550{
551 struct ipv6_txoptions *old = txopt_get(inet6_sk(sk)), *txopts;
552
553 txopts = ipv6_renew_options_kern(sk, old, IPV6_HOPOPTS,
554 hop, hop ? ipv6_optlen(hop) : 0);
555 txopt_put(old);
556 if (IS_ERR(txopts))
557 return PTR_ERR(txopts);
558
559 txopts = ipv6_update_options(sk, txopts);
560 if (txopts) {
561 atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
562 txopt_put(txopts);
563 }
564
565 return 0;
566}
567
568/**
569 * calipso_tlv_len - Returns the length of the TLV
570 * @opt: the option header
571 * @offset: offset of the TLV within the header
572 *
573 * Description:
574 * Returns the length of the TLV option at offset @offset within
575 * the option header @opt. Checks that the entire TLV fits inside
576 * the option header, returns a negative value if this is not the case.
577 */
578static int calipso_tlv_len(struct ipv6_opt_hdr *opt, unsigned int offset)
579{
580 unsigned char *tlv = (unsigned char *)opt;
581 unsigned int opt_len = ipv6_optlen(opt), tlv_len;
582
583 if (offset < sizeof(*opt) || offset >= opt_len)
584 return -EINVAL;
585 if (tlv[offset] == IPV6_TLV_PAD1)
586 return 1;
587 if (offset + 1 >= opt_len)
588 return -EINVAL;
589 tlv_len = tlv[offset + 1] + 2;
590 if (offset + tlv_len > opt_len)
591 return -EINVAL;
592 return tlv_len;
593}
594
595/**
596 * calipso_opt_find - Finds the CALIPSO option in an IPv6 hop options header
597 * @hop: the hop options header
598 * @start: on return holds the offset of any leading padding
599 * @end: on return holds the offset of the first non-pad TLV after CALIPSO
600 *
601 * Description:
602 * Finds the space occupied by a CALIPSO option (including any leading and
603 * trailing padding).
604 *
605 * If a CALIPSO option exists set @start and @end to the
606 * offsets within @hop of the start of padding before the first
607 * CALIPSO option and the end of padding after the first CALIPSO
608 * option. In this case the function returns 0.
609 *
610 * In the absence of a CALIPSO option, @start and @end will be
611 * set to the start and end of any trailing padding in the header.
612 * This is useful when appending a new option, as the caller may want
613 * to overwrite some of this padding. In this case the function will
614 * return -ENOENT.
615 */
616static int calipso_opt_find(struct ipv6_opt_hdr *hop, unsigned int *start,
617 unsigned int *end)
618{
619 int ret_val = -ENOENT, tlv_len;
620 unsigned int opt_len, offset, offset_s = 0, offset_e = 0;
621 unsigned char *opt = (unsigned char *)hop;
622
623 opt_len = ipv6_optlen(hop);
624 offset = sizeof(*hop);
625
626 while (offset < opt_len) {
627 tlv_len = calipso_tlv_len(hop, offset);
628 if (tlv_len < 0)
629 return tlv_len;
630
631 switch (opt[offset]) {
632 case IPV6_TLV_PAD1:
633 case IPV6_TLV_PADN:
634 if (offset_e)
635 offset_e = offset;
636 break;
637 case IPV6_TLV_CALIPSO:
638 ret_val = 0;
639 offset_e = offset;
640 break;
641 default:
642 if (offset_e == 0)
643 offset_s = offset;
644 else
645 goto out;
646 }
647 offset += tlv_len;
648 }
649
650out:
651 if (offset_s)
652 *start = offset_s + calipso_tlv_len(hop, offset_s);
653 else
654 *start = sizeof(*hop);
655 if (offset_e)
656 *end = offset_e + calipso_tlv_len(hop, offset_e);
657 else
658 *end = opt_len;
659
660 return ret_val;
661}
662
663/**
664 * calipso_opt_insert - Inserts a CALIPSO option into an IPv6 hop opt hdr
665 * @hop: the original hop options header
666 * @doi_def: the CALIPSO DOI to use
667 * @secattr: the specific security attributes of the socket
668 *
669 * Description:
670 * Creates a new hop options header based on @hop with a
671 * CALIPSO option added to it. If @hop already contains a CALIPSO
672 * option this is overwritten, otherwise the new option is appended
673 * after any existing options. If @hop is NULL then the new header
674 * will contain just the CALIPSO option and any needed padding.
675 *
676 */
677static struct ipv6_opt_hdr *
678calipso_opt_insert(struct ipv6_opt_hdr *hop,
679 const struct calipso_doi *doi_def,
680 const struct netlbl_lsm_secattr *secattr)
681{
682 unsigned int start, end, buf_len, pad, hop_len;
683 struct ipv6_opt_hdr *new;
684 int ret_val;
685
686 if (hop) {
687 hop_len = ipv6_optlen(hop);
688 ret_val = calipso_opt_find(hop, &start, &end);
689 if (ret_val && ret_val != -ENOENT)
690 return ERR_PTR(ret_val);
691 } else {
692 hop_len = 0;
693 start = sizeof(*hop);
694 end = 0;
695 }
696
697 buf_len = hop_len + start - end + CALIPSO_OPT_LEN_MAX_WITH_PAD;
698 new = kzalloc(buf_len, GFP_ATOMIC);
699 if (!new)
700 return ERR_PTR(-ENOMEM);
701
702 if (start > sizeof(*hop))
703 memcpy(new, hop, start);
704 ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
705 secattr);
706 if (ret_val < 0)
707 return ERR_PTR(ret_val);
708
709 buf_len = start + ret_val;
710 /* At this point buf_len aligns to 4n, so (buf_len & 4) pads to 8n */
711 pad = ((buf_len & 4) + (end & 7)) & 7;
712 calipso_pad_write((unsigned char *)new, buf_len, pad);
713 buf_len += pad;
714
715 if (end != hop_len) {
716 memcpy((char *)new + buf_len, (char *)hop + end, hop_len - end);
717 buf_len += hop_len - end;
718 }
719 new->nexthdr = 0;
720 new->hdrlen = buf_len / 8 - 1;
721
722 return new;
723}
724
725/**
726 * calipso_opt_del - Removes the CALIPSO option from an option header
727 * @hop: the original header
728 * @new: the new header
729 *
730 * Description:
731 * Creates a new header based on @hop without any CALIPSO option. If @hop
732 * doesn't contain a CALIPSO option it returns -ENOENT. If @hop contains
733 * no other non-padding options, it returns zero with @new set to NULL.
734 * Otherwise it returns zero, creates a new header without the CALIPSO
735 * option (and removing as much padding as possible) and returns with
736 * @new set to that header.
737 *
738 */
739static int calipso_opt_del(struct ipv6_opt_hdr *hop,
740 struct ipv6_opt_hdr **new)
741{
742 int ret_val;
743 unsigned int start, end, delta, pad, hop_len;
744
745 ret_val = calipso_opt_find(hop, &start, &end);
746 if (ret_val)
747 return ret_val;
748
749 hop_len = ipv6_optlen(hop);
750 if (start == sizeof(*hop) && end == hop_len) {
751 /* There's no other option in the header so return NULL */
752 *new = NULL;
753 return 0;
754 }
755
756 delta = (end - start) & ~7;
757 *new = kzalloc(hop_len - delta, GFP_ATOMIC);
758 if (!*new)
759 return -ENOMEM;
760
761 memcpy(*new, hop, start);
762 (*new)->hdrlen -= delta / 8;
763 pad = (end - start) & 7;
764 calipso_pad_write((unsigned char *)*new, start, pad);
765 if (end != hop_len)
766 memcpy((char *)*new + start + pad, (char *)hop + end,
767 hop_len - end);
768
769 return 0;
770}
771
772/**
773 * calipso_opt_getattr - Get the security attributes from a memory block
774 * @calipso: the CALIPSO option
775 * @secattr: the security attributes
776 *
777 * Description:
778 * Inspect @calipso and return the security attributes in @secattr.
779 * Returns zero on success and negative values on failure.
780 *
781 */
782static int calipso_opt_getattr(const unsigned char *calipso,
783 struct netlbl_lsm_secattr *secattr)
784{
785 int ret_val = -ENOMSG;
786 u32 doi, len = calipso[1], cat_len = calipso[6] * 4;
787 struct calipso_doi *doi_def;
788
789 if (cat_len + 8 > len)
790 return -EINVAL;
791
792 doi = get_unaligned_be32(calipso + 2);
793 rcu_read_lock();
794 doi_def = calipso_doi_search(doi);
795 if (!doi_def)
796 goto getattr_return;
797
798 secattr->attr.mls.lvl = calipso[7];
799 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
800
801 if (cat_len) {
802 ret_val = calipso_map_cat_ntoh(doi_def,
803 calipso + 10,
804 cat_len,
805 secattr);
806 if (ret_val != 0) {
807 netlbl_catmap_free(secattr->attr.mls.cat);
808 goto getattr_return;
809 }
810
811 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
812 }
813
814 secattr->type = NETLBL_NLTYPE_CALIPSO;
815
816getattr_return:
817 rcu_read_unlock();
818 return ret_val;
819}
820
821/* sock functions.
822 */
823
824/**
825 * calipso_sock_getattr - Get the security attributes from a sock
826 * @sk: the sock
827 * @secattr: the security attributes
828 *
829 * Description:
830 * Query @sk to see if there is a CALIPSO option attached to the sock and if
831 * there is return the CALIPSO security attributes in @secattr. This function
832 * requires that @sk be locked, or privately held, but it does not do any
833 * locking itself. Returns zero on success and negative values on failure.
834 *
835 */
836static int calipso_sock_getattr(struct sock *sk,
837 struct netlbl_lsm_secattr *secattr)
838{
839 struct ipv6_opt_hdr *hop;
840 int opt_len, len, ret_val = -ENOMSG, offset;
841 unsigned char *opt;
842 struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
843
844 if (!txopts || !txopts->hopopt)
845 goto done;
846
847 hop = txopts->hopopt;
848 opt = (unsigned char *)hop;
849 opt_len = ipv6_optlen(hop);
850 offset = sizeof(*hop);
851 while (offset < opt_len) {
852 len = calipso_tlv_len(hop, offset);
853 if (len < 0) {
854 ret_val = len;
855 goto done;
856 }
857 switch (opt[offset]) {
858 case IPV6_TLV_CALIPSO:
859 if (len < CALIPSO_HDR_LEN)
860 ret_val = -EINVAL;
861 else
862 ret_val = calipso_opt_getattr(&opt[offset],
863 secattr);
864 goto done;
865 default:
866 offset += len;
867 break;
868 }
869 }
870done:
871 txopt_put(txopts);
872 return ret_val;
873}
874
875/**
876 * calipso_sock_setattr - Add a CALIPSO option to a socket
877 * @sk: the socket
878 * @doi_def: the CALIPSO DOI to use
879 * @secattr: the specific security attributes of the socket
880 *
881 * Description:
882 * Set the CALIPSO option on the given socket using the DOI definition and
883 * security attributes passed to the function. This function requires
884 * exclusive access to @sk, which means it either needs to be in the
885 * process of being created or locked. Returns zero on success and negative
886 * values on failure.
887 *
888 */
889static int calipso_sock_setattr(struct sock *sk,
890 const struct calipso_doi *doi_def,
891 const struct netlbl_lsm_secattr *secattr)
892{
893 int ret_val;
894 struct ipv6_opt_hdr *old, *new;
895 struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
896
897 old = NULL;
898 if (txopts)
899 old = txopts->hopopt;
900
901 new = calipso_opt_insert(old, doi_def, secattr);
902 txopt_put(txopts);
903 if (IS_ERR(new))
904 return PTR_ERR(new);
905
906 ret_val = calipso_opt_update(sk, new);
907
908 kfree(new);
909 return ret_val;
910}
911
912/**
913 * calipso_sock_delattr - Delete the CALIPSO option from a socket
914 * @sk: the socket
915 *
916 * Description:
917 * Removes the CALIPSO option from a socket, if present.
918 *
919 */
920static void calipso_sock_delattr(struct sock *sk)
921{
922 struct ipv6_opt_hdr *new_hop;
923 struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
924
925 if (!txopts || !txopts->hopopt)
926 goto done;
927
928 if (calipso_opt_del(txopts->hopopt, &new_hop))
929 goto done;
930
931 calipso_opt_update(sk, new_hop);
932 kfree(new_hop);
933
934done:
935 txopt_put(txopts);
936}
937
Huw Daviese1adea92016-06-27 15:05:29 -0400938/* request sock functions.
939 */
940
941/**
942 * calipso_req_setattr - Add a CALIPSO option to a connection request socket
943 * @req: the connection request socket
944 * @doi_def: the CALIPSO DOI to use
945 * @secattr: the specific security attributes of the socket
946 *
947 * Description:
948 * Set the CALIPSO option on the given socket using the DOI definition and
949 * security attributes passed to the function. Returns zero on success and
950 * negative values on failure.
951 *
952 */
953static int calipso_req_setattr(struct request_sock *req,
954 const struct calipso_doi *doi_def,
955 const struct netlbl_lsm_secattr *secattr)
956{
957 struct ipv6_txoptions *txopts;
958 struct inet_request_sock *req_inet = inet_rsk(req);
959 struct ipv6_opt_hdr *old, *new;
960 struct sock *sk = sk_to_full_sk(req_to_sk(req));
961
962 if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
963 old = req_inet->ipv6_opt->hopopt;
964 else
965 old = NULL;
966
967 new = calipso_opt_insert(old, doi_def, secattr);
968 if (IS_ERR(new))
969 return PTR_ERR(new);
970
971 txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
972 new, new ? ipv6_optlen(new) : 0);
973
974 kfree(new);
975
976 if (IS_ERR(txopts))
977 return PTR_ERR(txopts);
978
979 txopts = xchg(&req_inet->ipv6_opt, txopts);
980 if (txopts) {
981 atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
982 txopt_put(txopts);
983 }
984
985 return 0;
986}
987
988/**
989 * calipso_req_delattr - Delete the CALIPSO option from a request socket
990 * @reg: the request socket
991 *
992 * Description:
993 * Removes the CALIPSO option from a request socket, if present.
994 *
995 */
996static void calipso_req_delattr(struct request_sock *req)
997{
998 struct inet_request_sock *req_inet = inet_rsk(req);
999 struct ipv6_opt_hdr *new;
1000 struct ipv6_txoptions *txopts;
1001 struct sock *sk = sk_to_full_sk(req_to_sk(req));
1002
1003 if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
1004 return;
1005
1006 if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
1007 return; /* Nothing to do */
1008
1009 txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
1010 new, new ? ipv6_optlen(new) : 0);
1011
1012 if (!IS_ERR(txopts)) {
1013 txopts = xchg(&req_inet->ipv6_opt, txopts);
1014 if (txopts) {
1015 atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
1016 txopt_put(txopts);
1017 }
1018 }
1019 kfree(new);
1020}
1021
Huw Davies2917f572016-06-27 15:06:15 -04001022/* skbuff functions.
1023 */
1024
1025/**
1026 * calipso_skbuff_optptr - Find the CALIPSO option in the packet
1027 * @skb: the packet
1028 *
1029 * Description:
1030 * Parse the packet's IP header looking for a CALIPSO option. Returns a pointer
1031 * to the start of the CALIPSO option on success, NULL if one if not found.
1032 *
1033 */
1034static unsigned char *calipso_skbuff_optptr(const struct sk_buff *skb)
1035{
1036 const struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);
1037 int offset;
1038
1039 if (ip6_hdr->nexthdr != NEXTHDR_HOP)
1040 return NULL;
1041
1042 offset = ipv6_find_tlv(skb, sizeof(*ip6_hdr), IPV6_TLV_CALIPSO);
1043 if (offset >= 0)
1044 return (unsigned char *)ip6_hdr + offset;
1045
1046 return NULL;
1047}
1048
1049/**
1050 * calipso_skbuff_setattr - Set the CALIPSO option on a packet
1051 * @skb: the packet
1052 * @doi_def: the CALIPSO DOI to use
1053 * @secattr: the security attributes
1054 *
1055 * Description:
1056 * Set the CALIPSO option on the given packet based on the security attributes.
1057 * Returns a pointer to the IP header on success and NULL on failure.
1058 *
1059 */
1060static int calipso_skbuff_setattr(struct sk_buff *skb,
1061 const struct calipso_doi *doi_def,
1062 const struct netlbl_lsm_secattr *secattr)
1063{
1064 int ret_val;
1065 struct ipv6hdr *ip6_hdr;
1066 struct ipv6_opt_hdr *hop;
1067 unsigned char buf[CALIPSO_MAX_BUFFER];
1068 int len_delta, new_end, pad;
1069 unsigned int start, end;
1070
1071 ip6_hdr = ipv6_hdr(skb);
1072 if (ip6_hdr->nexthdr == NEXTHDR_HOP) {
1073 hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
1074 ret_val = calipso_opt_find(hop, &start, &end);
1075 if (ret_val && ret_val != -ENOENT)
1076 return ret_val;
1077 } else {
1078 start = 0;
1079 end = 0;
1080 }
1081
1082 memset(buf, 0, sizeof(buf));
1083 ret_val = calipso_genopt(buf, start & 3, sizeof(buf), doi_def, secattr);
1084 if (ret_val < 0)
1085 return ret_val;
1086
1087 new_end = start + ret_val;
1088 /* At this point new_end aligns to 4n, so (new_end & 4) pads to 8n */
1089 pad = ((new_end & 4) + (end & 7)) & 7;
1090 len_delta = new_end - (int)end + pad;
1091 ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
1092 if (ret_val < 0)
1093 return ret_val;
1094
1095 if (len_delta) {
1096 if (len_delta > 0)
1097 skb_push(skb, len_delta);
1098 else
1099 skb_pull(skb, -len_delta);
1100 memmove((char *)ip6_hdr - len_delta, ip6_hdr,
1101 sizeof(*ip6_hdr) + start);
1102 skb_reset_network_header(skb);
1103 ip6_hdr = ipv6_hdr(skb);
1104 }
1105
1106 hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
1107 if (start == 0) {
1108 struct ipv6_opt_hdr *new_hop = (struct ipv6_opt_hdr *)buf;
1109
1110 new_hop->nexthdr = ip6_hdr->nexthdr;
1111 new_hop->hdrlen = len_delta / 8 - 1;
1112 ip6_hdr->nexthdr = NEXTHDR_HOP;
1113 } else {
1114 hop->hdrlen += len_delta / 8;
1115 }
1116 memcpy((char *)hop + start, buf + (start & 3), new_end - start);
1117 calipso_pad_write((unsigned char *)hop, new_end, pad);
1118
1119 return 0;
1120}
1121
1122/**
1123 * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
1124 * @skb: the packet
1125 *
1126 * Description:
1127 * Removes any and all CALIPSO options from the given packet. Returns zero on
1128 * success, negative values on failure.
1129 *
1130 */
1131static int calipso_skbuff_delattr(struct sk_buff *skb)
1132{
1133 int ret_val;
1134 struct ipv6hdr *ip6_hdr;
1135 struct ipv6_opt_hdr *old_hop;
1136 u32 old_hop_len, start = 0, end = 0, delta, size, pad;
1137
1138 if (!calipso_skbuff_optptr(skb))
1139 return 0;
1140
1141 /* since we are changing the packet we should make a copy */
1142 ret_val = skb_cow(skb, skb_headroom(skb));
1143 if (ret_val < 0)
1144 return ret_val;
1145
1146 ip6_hdr = ipv6_hdr(skb);
1147 old_hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
1148 old_hop_len = ipv6_optlen(old_hop);
1149
1150 ret_val = calipso_opt_find(old_hop, &start, &end);
1151 if (ret_val)
1152 return ret_val;
1153
1154 if (start == sizeof(*old_hop) && end == old_hop_len) {
1155 /* There's no other option in the header so we delete
1156 * the whole thing. */
1157 delta = old_hop_len;
1158 size = sizeof(*ip6_hdr);
1159 ip6_hdr->nexthdr = old_hop->nexthdr;
1160 } else {
1161 delta = (end - start) & ~7;
1162 if (delta)
1163 old_hop->hdrlen -= delta / 8;
1164 pad = (end - start) & 7;
1165 size = sizeof(*ip6_hdr) + start + pad;
1166 calipso_pad_write((unsigned char *)old_hop, start, pad);
1167 }
1168
1169 if (delta) {
1170 skb_pull(skb, delta);
1171 memmove((char *)ip6_hdr + delta, ip6_hdr, size);
1172 skb_reset_network_header(skb);
1173 }
1174
1175 return 0;
1176}
1177
Huw Daviescb72d382016-06-27 15:02:46 -04001178static const struct netlbl_calipso_ops ops = {
1179 .doi_add = calipso_doi_add,
1180 .doi_free = calipso_doi_free,
Huw Daviesd7cce012016-06-27 15:02:49 -04001181 .doi_remove = calipso_doi_remove,
Huw Daviesa5e34492016-06-27 15:02:47 -04001182 .doi_getdef = calipso_doi_getdef,
1183 .doi_putdef = calipso_doi_putdef,
Huw Daviese1ce69d2016-06-27 15:02:48 -04001184 .doi_walk = calipso_doi_walk,
Huw Daviesceba1832016-06-27 15:02:51 -04001185 .sock_getattr = calipso_sock_getattr,
1186 .sock_setattr = calipso_sock_setattr,
1187 .sock_delattr = calipso_sock_delattr,
Huw Daviese1adea92016-06-27 15:05:29 -04001188 .req_setattr = calipso_req_setattr,
1189 .req_delattr = calipso_req_delattr,
Huw Davies2917f572016-06-27 15:06:15 -04001190 .opt_getattr = calipso_opt_getattr,
1191 .skbuff_optptr = calipso_skbuff_optptr,
1192 .skbuff_setattr = calipso_skbuff_setattr,
1193 .skbuff_delattr = calipso_skbuff_delattr,
Huw Daviescb72d382016-06-27 15:02:46 -04001194};
1195
1196/**
1197 * calipso_init - Initialize the CALIPSO module
1198 *
1199 * Description:
1200 * Initialize the CALIPSO module and prepare it for use. Returns zero on
1201 * success and negative values on failure.
1202 *
1203 */
1204int __init calipso_init(void)
1205{
1206 netlbl_calipso_ops_register(&ops);
1207 return 0;
1208}
1209
1210void calipso_exit(void)
1211{
1212 netlbl_calipso_ops_register(NULL);
1213}