netlabel_unlabeled.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /*
  2. * NetLabel Unlabeled Support
  3. *
  4. * This file defines functions for dealing with unlabeled packets for the
  5. * NetLabel system. The NetLabel system manages static and dynamic label
  6. * mappings for network protocols such as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/types.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/socket.h>
  34. #include <linux/string.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/audit.h>
  37. #include <linux/in.h>
  38. #include <linux/in6.h>
  39. #include <linux/ip.h>
  40. #include <linux/ipv6.h>
  41. #include <linux/notifier.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/security.h>
  44. #include <net/sock.h>
  45. #include <net/netlink.h>
  46. #include <net/genetlink.h>
  47. #include <net/ip.h>
  48. #include <net/ipv6.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netlabel.h>
  51. #include <asm/bug.h>
  52. #include <asm/atomic.h>
  53. #include "netlabel_user.h"
  54. #include "netlabel_addrlist.h"
  55. #include "netlabel_domainhash.h"
  56. #include "netlabel_unlabeled.h"
  57. #include "netlabel_mgmt.h"
  58. /* NOTE: at present we always use init's network namespace since we don't
  59. * presently support different namespaces even though the majority of
  60. * the functions in this file are "namespace safe" */
  61. /* The unlabeled connection hash table which we use to map network interfaces
  62. * and addresses of unlabeled packets to a user specified secid value for the
  63. * LSM. The hash table is used to lookup the network interface entry
  64. * (struct netlbl_unlhsh_iface) and then the interface entry is used to
  65. * lookup an IP address match from an ordered list. If a network interface
  66. * match can not be found in the hash table then the default entry
  67. * (netlbl_unlhsh_def) is used. The IP address entry list
  68. * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
  69. * larger netmask come first.
  70. */
  71. struct netlbl_unlhsh_tbl {
  72. struct list_head *tbl;
  73. u32 size;
  74. };
  75. #define netlbl_unlhsh_addr4_entry(iter) \
  76. container_of(iter, struct netlbl_unlhsh_addr4, list)
  77. struct netlbl_unlhsh_addr4 {
  78. u32 secid;
  79. struct netlbl_af4list list;
  80. struct rcu_head rcu;
  81. };
  82. #define netlbl_unlhsh_addr6_entry(iter) \
  83. container_of(iter, struct netlbl_unlhsh_addr6, list)
  84. struct netlbl_unlhsh_addr6 {
  85. u32 secid;
  86. struct netlbl_af6list list;
  87. struct rcu_head rcu;
  88. };
  89. struct netlbl_unlhsh_iface {
  90. int ifindex;
  91. struct list_head addr4_list;
  92. struct list_head addr6_list;
  93. u32 valid;
  94. struct list_head list;
  95. struct rcu_head rcu;
  96. };
  97. /* Argument struct for netlbl_unlhsh_walk() */
  98. struct netlbl_unlhsh_walk_arg {
  99. struct netlink_callback *nl_cb;
  100. struct sk_buff *skb;
  101. u32 seq;
  102. };
  103. /* Unlabeled connection hash table */
  104. /* updates should be so rare that having one spinlock for the entire
  105. * hash table should be okay */
  106. static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
  107. static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
  108. static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
  109. /* Accept unlabeled packets flag */
  110. static u8 netlabel_unlabel_acceptflg = 0;
  111. /* NetLabel Generic NETLINK unlabeled family */
  112. static struct genl_family netlbl_unlabel_gnl_family = {
  113. .id = GENL_ID_GENERATE,
  114. .hdrsize = 0,
  115. .name = NETLBL_NLTYPE_UNLABELED_NAME,
  116. .version = NETLBL_PROTO_VERSION,
  117. .maxattr = NLBL_UNLABEL_A_MAX,
  118. };
  119. /* NetLabel Netlink attribute policy */
  120. static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
  121. [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
  122. [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
  123. .len = sizeof(struct in6_addr) },
  124. [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
  125. .len = sizeof(struct in6_addr) },
  126. [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
  127. .len = sizeof(struct in_addr) },
  128. [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
  129. .len = sizeof(struct in_addr) },
  130. [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
  131. .len = IFNAMSIZ - 1 },
  132. [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
  133. };
  134. /*
  135. * Unlabeled Connection Hash Table Functions
  136. */
  137. /**
  138. * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
  139. * @entry: the entry's RCU field
  140. *
  141. * Description:
  142. * This function is designed to be used as a callback to the call_rcu()
  143. * function so that memory allocated to a hash table address entry can be
  144. * released safely.
  145. *
  146. */
  147. static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
  148. {
  149. struct netlbl_unlhsh_addr4 *ptr;
  150. ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
  151. kfree(ptr);
  152. }
  153. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  154. /**
  155. * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
  156. * @entry: the entry's RCU field
  157. *
  158. * Description:
  159. * This function is designed to be used as a callback to the call_rcu()
  160. * function so that memory allocated to a hash table address entry can be
  161. * released safely.
  162. *
  163. */
  164. static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
  165. {
  166. struct netlbl_unlhsh_addr6 *ptr;
  167. ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
  168. kfree(ptr);
  169. }
  170. #endif /* IPv6 */
  171. /**
  172. * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
  173. * @entry: the entry's RCU field
  174. *
  175. * Description:
  176. * This function is designed to be used as a callback to the call_rcu()
  177. * function so that memory allocated to a hash table interface entry can be
  178. * released safely. It is important to note that this function does not free
  179. * the IPv4 and IPv6 address lists contained as part of an interface entry. It
  180. * is up to the rest of the code to make sure an interface entry is only freed
  181. * once it's address lists are empty.
  182. *
  183. */
  184. static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
  185. {
  186. struct netlbl_unlhsh_iface *iface;
  187. struct netlbl_af4list *iter4;
  188. struct netlbl_af4list *tmp4;
  189. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  190. struct netlbl_af6list *iter6;
  191. struct netlbl_af6list *tmp6;
  192. #endif /* IPv6 */
  193. iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
  194. /* no need for locks here since we are the only one with access to this
  195. * structure */
  196. netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
  197. netlbl_af4list_remove_entry(iter4);
  198. kfree(netlbl_unlhsh_addr4_entry(iter4));
  199. }
  200. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  201. netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
  202. netlbl_af6list_remove_entry(iter6);
  203. kfree(netlbl_unlhsh_addr6_entry(iter6));
  204. }
  205. #endif /* IPv6 */
  206. kfree(iface);
  207. }
  208. /**
  209. * netlbl_unlhsh_hash - Hashing function for the hash table
  210. * @ifindex: the network interface/device to hash
  211. *
  212. * Description:
  213. * This is the hashing function for the unlabeled hash table, it returns the
  214. * bucket number for the given device/interface. The caller is responsible for
  215. * calling the rcu_read_[un]lock() functions.
  216. *
  217. */
  218. static u32 netlbl_unlhsh_hash(int ifindex)
  219. {
  220. /* this is taken _almost_ directly from
  221. * security/selinux/netif.c:sel_netif_hasfn() as they do pretty much
  222. * the same thing */
  223. return ifindex & (rcu_dereference(netlbl_unlhsh)->size - 1);
  224. }
  225. /**
  226. * netlbl_unlhsh_search_iface - Search for a matching interface entry
  227. * @ifindex: the network interface
  228. *
  229. * Description:
  230. * Searches the unlabeled connection hash table and returns a pointer to the
  231. * interface entry which matches @ifindex, otherwise NULL is returned. The
  232. * caller is responsible for calling the rcu_read_[un]lock() functions.
  233. *
  234. */
  235. static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
  236. {
  237. u32 bkt;
  238. struct list_head *bkt_list;
  239. struct netlbl_unlhsh_iface *iter;
  240. bkt = netlbl_unlhsh_hash(ifindex);
  241. bkt_list = &rcu_dereference(netlbl_unlhsh)->tbl[bkt];
  242. list_for_each_entry_rcu(iter, bkt_list, list)
  243. if (iter->valid && iter->ifindex == ifindex)
  244. return iter;
  245. return NULL;
  246. }
  247. /**
  248. * netlbl_unlhsh_search_iface_def - Search for a matching interface entry
  249. * @ifindex: the network interface
  250. *
  251. * Description:
  252. * Searches the unlabeled connection hash table and returns a pointer to the
  253. * interface entry which matches @ifindex. If an exact match can not be found
  254. * and there is a valid default entry, the default entry is returned, otherwise
  255. * NULL is returned. The caller is responsible for calling the
  256. * rcu_read_[un]lock() functions.
  257. *
  258. */
  259. static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface_def(int ifindex)
  260. {
  261. struct netlbl_unlhsh_iface *entry;
  262. entry = netlbl_unlhsh_search_iface(ifindex);
  263. if (entry != NULL)
  264. return entry;
  265. entry = rcu_dereference(netlbl_unlhsh_def);
  266. if (entry != NULL && entry->valid)
  267. return entry;
  268. return NULL;
  269. }
  270. /**
  271. * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
  272. * @iface: the associated interface entry
  273. * @addr: IPv4 address in network byte order
  274. * @mask: IPv4 address mask in network byte order
  275. * @secid: LSM secid value for entry
  276. *
  277. * Description:
  278. * Add a new address entry into the unlabeled connection hash table using the
  279. * interface entry specified by @iface. On success zero is returned, otherwise
  280. * a negative value is returned. The caller is responsible for calling the
  281. * rcu_read_[un]lock() functions.
  282. *
  283. */
  284. static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
  285. const struct in_addr *addr,
  286. const struct in_addr *mask,
  287. u32 secid)
  288. {
  289. int ret_val;
  290. struct netlbl_unlhsh_addr4 *entry;
  291. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  292. if (entry == NULL)
  293. return -ENOMEM;
  294. entry->list.addr = addr->s_addr & mask->s_addr;
  295. entry->list.mask = mask->s_addr;
  296. entry->list.valid = 1;
  297. INIT_RCU_HEAD(&entry->rcu);
  298. entry->secid = secid;
  299. spin_lock(&netlbl_unlhsh_lock);
  300. ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
  301. spin_unlock(&netlbl_unlhsh_lock);
  302. if (ret_val != 0)
  303. kfree(entry);
  304. return ret_val;
  305. }
  306. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  307. /**
  308. * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
  309. * @iface: the associated interface entry
  310. * @addr: IPv6 address in network byte order
  311. * @mask: IPv6 address mask in network byte order
  312. * @secid: LSM secid value for entry
  313. *
  314. * Description:
  315. * Add a new address entry into the unlabeled connection hash table using the
  316. * interface entry specified by @iface. On success zero is returned, otherwise
  317. * a negative value is returned. The caller is responsible for calling the
  318. * rcu_read_[un]lock() functions.
  319. *
  320. */
  321. static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
  322. const struct in6_addr *addr,
  323. const struct in6_addr *mask,
  324. u32 secid)
  325. {
  326. int ret_val;
  327. struct netlbl_unlhsh_addr6 *entry;
  328. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  329. if (entry == NULL)
  330. return -ENOMEM;
  331. ipv6_addr_copy(&entry->list.addr, addr);
  332. entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
  333. entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
  334. entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
  335. entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
  336. ipv6_addr_copy(&entry->list.mask, mask);
  337. entry->list.valid = 1;
  338. INIT_RCU_HEAD(&entry->rcu);
  339. entry->secid = secid;
  340. spin_lock(&netlbl_unlhsh_lock);
  341. ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
  342. spin_unlock(&netlbl_unlhsh_lock);
  343. if (ret_val != 0)
  344. kfree(entry);
  345. return 0;
  346. }
  347. #endif /* IPv6 */
  348. /**
  349. * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
  350. * @ifindex: network interface
  351. *
  352. * Description:
  353. * Add a new, empty, interface entry into the unlabeled connection hash table.
  354. * On success a pointer to the new interface entry is returned, on failure NULL
  355. * is returned. The caller is responsible for calling the rcu_read_[un]lock()
  356. * functions.
  357. *
  358. */
  359. static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
  360. {
  361. u32 bkt;
  362. struct netlbl_unlhsh_iface *iface;
  363. iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
  364. if (iface == NULL)
  365. return NULL;
  366. iface->ifindex = ifindex;
  367. INIT_LIST_HEAD(&iface->addr4_list);
  368. INIT_LIST_HEAD(&iface->addr6_list);
  369. iface->valid = 1;
  370. INIT_RCU_HEAD(&iface->rcu);
  371. spin_lock(&netlbl_unlhsh_lock);
  372. if (ifindex > 0) {
  373. bkt = netlbl_unlhsh_hash(ifindex);
  374. if (netlbl_unlhsh_search_iface(ifindex) != NULL)
  375. goto add_iface_failure;
  376. list_add_tail_rcu(&iface->list,
  377. &rcu_dereference(netlbl_unlhsh)->tbl[bkt]);
  378. } else {
  379. INIT_LIST_HEAD(&iface->list);
  380. if (rcu_dereference(netlbl_unlhsh_def) != NULL)
  381. goto add_iface_failure;
  382. rcu_assign_pointer(netlbl_unlhsh_def, iface);
  383. }
  384. spin_unlock(&netlbl_unlhsh_lock);
  385. return iface;
  386. add_iface_failure:
  387. spin_unlock(&netlbl_unlhsh_lock);
  388. kfree(iface);
  389. return NULL;
  390. }
  391. /**
  392. * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
  393. * @net: network namespace
  394. * @dev_name: interface name
  395. * @addr: IP address in network byte order
  396. * @mask: address mask in network byte order
  397. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  398. * @secid: LSM secid value for the entry
  399. * @audit_info: NetLabel audit information
  400. *
  401. * Description:
  402. * Adds a new entry to the unlabeled connection hash table. Returns zero on
  403. * success, negative values on failure.
  404. *
  405. */
  406. int netlbl_unlhsh_add(struct net *net,
  407. const char *dev_name,
  408. const void *addr,
  409. const void *mask,
  410. u32 addr_len,
  411. u32 secid,
  412. struct netlbl_audit *audit_info)
  413. {
  414. int ret_val;
  415. int ifindex;
  416. struct net_device *dev;
  417. struct netlbl_unlhsh_iface *iface;
  418. struct audit_buffer *audit_buf = NULL;
  419. char *secctx = NULL;
  420. u32 secctx_len;
  421. if (addr_len != sizeof(struct in_addr) &&
  422. addr_len != sizeof(struct in6_addr))
  423. return -EINVAL;
  424. rcu_read_lock();
  425. if (dev_name != NULL) {
  426. dev = dev_get_by_name(net, dev_name);
  427. if (dev == NULL) {
  428. ret_val = -ENODEV;
  429. goto unlhsh_add_return;
  430. }
  431. ifindex = dev->ifindex;
  432. dev_put(dev);
  433. iface = netlbl_unlhsh_search_iface(ifindex);
  434. } else {
  435. ifindex = 0;
  436. iface = rcu_dereference(netlbl_unlhsh_def);
  437. }
  438. if (iface == NULL) {
  439. iface = netlbl_unlhsh_add_iface(ifindex);
  440. if (iface == NULL) {
  441. ret_val = -ENOMEM;
  442. goto unlhsh_add_return;
  443. }
  444. }
  445. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
  446. audit_info);
  447. switch (addr_len) {
  448. case sizeof(struct in_addr): {
  449. struct in_addr *addr4, *mask4;
  450. addr4 = (struct in_addr *)addr;
  451. mask4 = (struct in_addr *)mask;
  452. ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
  453. if (audit_buf != NULL)
  454. netlbl_af4list_audit_addr(audit_buf, 1,
  455. dev_name,
  456. addr4->s_addr,
  457. mask4->s_addr);
  458. break;
  459. }
  460. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  461. case sizeof(struct in6_addr): {
  462. struct in6_addr *addr6, *mask6;
  463. addr6 = (struct in6_addr *)addr;
  464. mask6 = (struct in6_addr *)mask;
  465. ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
  466. if (audit_buf != NULL)
  467. netlbl_af6list_audit_addr(audit_buf, 1,
  468. dev_name,
  469. addr6, mask6);
  470. break;
  471. }
  472. #endif /* IPv6 */
  473. default:
  474. ret_val = -EINVAL;
  475. }
  476. if (ret_val == 0)
  477. atomic_inc(&netlabel_mgmt_protocount);
  478. unlhsh_add_return:
  479. rcu_read_unlock();
  480. if (audit_buf != NULL) {
  481. if (security_secid_to_secctx(secid,
  482. &secctx,
  483. &secctx_len) == 0) {
  484. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  485. security_release_secctx(secctx, secctx_len);
  486. }
  487. audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
  488. audit_log_end(audit_buf);
  489. }
  490. return ret_val;
  491. }
  492. /**
  493. * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
  494. * @net: network namespace
  495. * @iface: interface entry
  496. * @addr: IP address
  497. * @mask: IP address mask
  498. * @audit_info: NetLabel audit information
  499. *
  500. * Description:
  501. * Remove an IP address entry from the unlabeled connection hash table.
  502. * Returns zero on success, negative values on failure. The caller is
  503. * responsible for calling the rcu_read_[un]lock() functions.
  504. *
  505. */
  506. static int netlbl_unlhsh_remove_addr4(struct net *net,
  507. struct netlbl_unlhsh_iface *iface,
  508. const struct in_addr *addr,
  509. const struct in_addr *mask,
  510. struct netlbl_audit *audit_info)
  511. {
  512. struct netlbl_af4list *list_entry;
  513. struct netlbl_unlhsh_addr4 *entry;
  514. struct audit_buffer *audit_buf;
  515. struct net_device *dev;
  516. char *secctx;
  517. u32 secctx_len;
  518. spin_lock(&netlbl_unlhsh_lock);
  519. list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  520. &iface->addr4_list);
  521. spin_unlock(&netlbl_unlhsh_lock);
  522. if (list_entry != NULL)
  523. entry = netlbl_unlhsh_addr4_entry(list_entry);
  524. else
  525. entry = NULL;
  526. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  527. audit_info);
  528. if (audit_buf != NULL) {
  529. dev = dev_get_by_index(net, iface->ifindex);
  530. netlbl_af4list_audit_addr(audit_buf, 1,
  531. (dev != NULL ? dev->name : NULL),
  532. addr->s_addr, mask->s_addr);
  533. if (dev != NULL)
  534. dev_put(dev);
  535. if (entry != NULL &&
  536. security_secid_to_secctx(entry->secid,
  537. &secctx, &secctx_len) == 0) {
  538. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  539. security_release_secctx(secctx, secctx_len);
  540. }
  541. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  542. audit_log_end(audit_buf);
  543. }
  544. if (entry == NULL)
  545. return -ENOENT;
  546. call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
  547. return 0;
  548. }
  549. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  550. /**
  551. * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
  552. * @net: network namespace
  553. * @iface: interface entry
  554. * @addr: IP address
  555. * @mask: IP address mask
  556. * @audit_info: NetLabel audit information
  557. *
  558. * Description:
  559. * Remove an IP address entry from the unlabeled connection hash table.
  560. * Returns zero on success, negative values on failure. The caller is
  561. * responsible for calling the rcu_read_[un]lock() functions.
  562. *
  563. */
  564. static int netlbl_unlhsh_remove_addr6(struct net *net,
  565. struct netlbl_unlhsh_iface *iface,
  566. const struct in6_addr *addr,
  567. const struct in6_addr *mask,
  568. struct netlbl_audit *audit_info)
  569. {
  570. struct netlbl_af6list *list_entry;
  571. struct netlbl_unlhsh_addr6 *entry;
  572. struct audit_buffer *audit_buf;
  573. struct net_device *dev;
  574. char *secctx;
  575. u32 secctx_len;
  576. spin_lock(&netlbl_unlhsh_lock);
  577. list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
  578. spin_unlock(&netlbl_unlhsh_lock);
  579. if (list_entry != NULL)
  580. entry = netlbl_unlhsh_addr6_entry(list_entry);
  581. else
  582. entry = NULL;
  583. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  584. audit_info);
  585. if (audit_buf != NULL) {
  586. dev = dev_get_by_index(net, iface->ifindex);
  587. netlbl_af6list_audit_addr(audit_buf, 1,
  588. (dev != NULL ? dev->name : NULL),
  589. addr, mask);
  590. if (dev != NULL)
  591. dev_put(dev);
  592. if (entry != NULL &&
  593. security_secid_to_secctx(entry->secid,
  594. &secctx, &secctx_len) == 0) {
  595. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  596. security_release_secctx(secctx, secctx_len);
  597. }
  598. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  599. audit_log_end(audit_buf);
  600. }
  601. if (entry == NULL)
  602. return -ENOENT;
  603. call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
  604. return 0;
  605. }
  606. #endif /* IPv6 */
  607. /**
  608. * netlbl_unlhsh_condremove_iface - Remove an interface entry
  609. * @iface: the interface entry
  610. *
  611. * Description:
  612. * Remove an interface entry from the unlabeled connection hash table if it is
  613. * empty. An interface entry is considered to be empty if there are no
  614. * address entries assigned to it.
  615. *
  616. */
  617. static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
  618. {
  619. struct netlbl_af4list *iter4;
  620. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  621. struct netlbl_af6list *iter6;
  622. #endif /* IPv6 */
  623. spin_lock(&netlbl_unlhsh_lock);
  624. netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
  625. goto unlhsh_condremove_failure;
  626. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  627. netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
  628. goto unlhsh_condremove_failure;
  629. #endif /* IPv6 */
  630. iface->valid = 0;
  631. if (iface->ifindex > 0)
  632. list_del_rcu(&iface->list);
  633. else
  634. rcu_assign_pointer(netlbl_unlhsh_def, NULL);
  635. spin_unlock(&netlbl_unlhsh_lock);
  636. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  637. return;
  638. unlhsh_condremove_failure:
  639. spin_unlock(&netlbl_unlhsh_lock);
  640. return;
  641. }
  642. /**
  643. * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
  644. * @net: network namespace
  645. * @dev_name: interface name
  646. * @addr: IP address in network byte order
  647. * @mask: address mask in network byte order
  648. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  649. * @audit_info: NetLabel audit information
  650. *
  651. * Description:
  652. * Removes and existing entry from the unlabeled connection hash table.
  653. * Returns zero on success, negative values on failure.
  654. *
  655. */
  656. int netlbl_unlhsh_remove(struct net *net,
  657. const char *dev_name,
  658. const void *addr,
  659. const void *mask,
  660. u32 addr_len,
  661. struct netlbl_audit *audit_info)
  662. {
  663. int ret_val;
  664. struct net_device *dev;
  665. struct netlbl_unlhsh_iface *iface;
  666. if (addr_len != sizeof(struct in_addr) &&
  667. addr_len != sizeof(struct in6_addr))
  668. return -EINVAL;
  669. rcu_read_lock();
  670. if (dev_name != NULL) {
  671. dev = dev_get_by_name(net, dev_name);
  672. if (dev == NULL) {
  673. ret_val = -ENODEV;
  674. goto unlhsh_remove_return;
  675. }
  676. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  677. dev_put(dev);
  678. } else
  679. iface = rcu_dereference(netlbl_unlhsh_def);
  680. if (iface == NULL) {
  681. ret_val = -ENOENT;
  682. goto unlhsh_remove_return;
  683. }
  684. switch (addr_len) {
  685. case sizeof(struct in_addr):
  686. ret_val = netlbl_unlhsh_remove_addr4(net,
  687. iface, addr, mask,
  688. audit_info);
  689. break;
  690. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  691. case sizeof(struct in6_addr):
  692. ret_val = netlbl_unlhsh_remove_addr6(net,
  693. iface, addr, mask,
  694. audit_info);
  695. break;
  696. #endif /* IPv6 */
  697. default:
  698. ret_val = -EINVAL;
  699. }
  700. if (ret_val == 0) {
  701. netlbl_unlhsh_condremove_iface(iface);
  702. atomic_dec(&netlabel_mgmt_protocount);
  703. }
  704. unlhsh_remove_return:
  705. rcu_read_unlock();
  706. return ret_val;
  707. }
  708. /*
  709. * General Helper Functions
  710. */
  711. /**
  712. * netlbl_unlhsh_netdev_handler - Network device notification handler
  713. * @this: notifier block
  714. * @event: the event
  715. * @ptr: the network device (cast to void)
  716. *
  717. * Description:
  718. * Handle network device events, although at present all we care about is a
  719. * network device going away. In the case of a device going away we clear any
  720. * related entries from the unlabeled connection hash table.
  721. *
  722. */
  723. static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
  724. unsigned long event,
  725. void *ptr)
  726. {
  727. struct net_device *dev = ptr;
  728. struct netlbl_unlhsh_iface *iface = NULL;
  729. if (!net_eq(dev_net(dev), &init_net))
  730. return NOTIFY_DONE;
  731. /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
  732. if (event == NETDEV_DOWN) {
  733. spin_lock(&netlbl_unlhsh_lock);
  734. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  735. if (iface != NULL && iface->valid) {
  736. iface->valid = 0;
  737. list_del_rcu(&iface->list);
  738. } else
  739. iface = NULL;
  740. spin_unlock(&netlbl_unlhsh_lock);
  741. }
  742. if (iface != NULL)
  743. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  744. return NOTIFY_DONE;
  745. }
  746. /**
  747. * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
  748. * @value: desired value
  749. * @audit_info: NetLabel audit information
  750. *
  751. * Description:
  752. * Set the value of the unlabeled accept flag to @value.
  753. *
  754. */
  755. static void netlbl_unlabel_acceptflg_set(u8 value,
  756. struct netlbl_audit *audit_info)
  757. {
  758. struct audit_buffer *audit_buf;
  759. u8 old_val;
  760. old_val = netlabel_unlabel_acceptflg;
  761. netlabel_unlabel_acceptflg = value;
  762. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
  763. audit_info);
  764. if (audit_buf != NULL) {
  765. audit_log_format(audit_buf,
  766. " unlbl_accept=%u old=%u", value, old_val);
  767. audit_log_end(audit_buf);
  768. }
  769. }
  770. /**
  771. * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
  772. * @info: the Generic NETLINK info block
  773. * @addr: the IP address
  774. * @mask: the IP address mask
  775. * @len: the address length
  776. *
  777. * Description:
  778. * Examine the Generic NETLINK message and extract the IP address information.
  779. * Returns zero on success, negative values on failure.
  780. *
  781. */
  782. static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
  783. void **addr,
  784. void **mask,
  785. u32 *len)
  786. {
  787. u32 addr_len;
  788. if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
  789. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  790. if (addr_len != sizeof(struct in_addr) &&
  791. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
  792. return -EINVAL;
  793. *len = addr_len;
  794. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  795. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
  796. return 0;
  797. } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
  798. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  799. if (addr_len != sizeof(struct in6_addr) &&
  800. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
  801. return -EINVAL;
  802. *len = addr_len;
  803. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  804. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
  805. return 0;
  806. }
  807. return -EINVAL;
  808. }
  809. /*
  810. * NetLabel Command Handlers
  811. */
  812. /**
  813. * netlbl_unlabel_accept - Handle an ACCEPT message
  814. * @skb: the NETLINK buffer
  815. * @info: the Generic NETLINK info block
  816. *
  817. * Description:
  818. * Process a user generated ACCEPT message and set the accept flag accordingly.
  819. * Returns zero on success, negative values on failure.
  820. *
  821. */
  822. static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  823. {
  824. u8 value;
  825. struct netlbl_audit audit_info;
  826. if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
  827. value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
  828. if (value == 1 || value == 0) {
  829. netlbl_netlink_auditinfo(skb, &audit_info);
  830. netlbl_unlabel_acceptflg_set(value, &audit_info);
  831. return 0;
  832. }
  833. }
  834. return -EINVAL;
  835. }
  836. /**
  837. * netlbl_unlabel_list - Handle a LIST message
  838. * @skb: the NETLINK buffer
  839. * @info: the Generic NETLINK info block
  840. *
  841. * Description:
  842. * Process a user generated LIST message and respond with the current status.
  843. * Returns zero on success, negative values on failure.
  844. *
  845. */
  846. static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  847. {
  848. int ret_val = -EINVAL;
  849. struct sk_buff *ans_skb;
  850. void *data;
  851. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  852. if (ans_skb == NULL)
  853. goto list_failure;
  854. data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
  855. 0, NLBL_UNLABEL_C_LIST);
  856. if (data == NULL) {
  857. ret_val = -ENOMEM;
  858. goto list_failure;
  859. }
  860. ret_val = nla_put_u8(ans_skb,
  861. NLBL_UNLABEL_A_ACPTFLG,
  862. netlabel_unlabel_acceptflg);
  863. if (ret_val != 0)
  864. goto list_failure;
  865. genlmsg_end(ans_skb, data);
  866. return genlmsg_reply(ans_skb, info);
  867. list_failure:
  868. kfree_skb(ans_skb);
  869. return ret_val;
  870. }
  871. /**
  872. * netlbl_unlabel_staticadd - Handle a STATICADD message
  873. * @skb: the NETLINK buffer
  874. * @info: the Generic NETLINK info block
  875. *
  876. * Description:
  877. * Process a user generated STATICADD message and add a new unlabeled
  878. * connection entry to the hash table. Returns zero on success, negative
  879. * values on failure.
  880. *
  881. */
  882. static int netlbl_unlabel_staticadd(struct sk_buff *skb,
  883. struct genl_info *info)
  884. {
  885. int ret_val;
  886. char *dev_name;
  887. void *addr;
  888. void *mask;
  889. u32 addr_len;
  890. u32 secid;
  891. struct netlbl_audit audit_info;
  892. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  893. * single entry. However, allow users to create two entries, one each
  894. * for IPv4 and IPv4, with the same LSM security context which should
  895. * achieve the same result. */
  896. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  897. !info->attrs[NLBL_UNLABEL_A_IFACE] ||
  898. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  899. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  900. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  901. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  902. return -EINVAL;
  903. netlbl_netlink_auditinfo(skb, &audit_info);
  904. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  905. if (ret_val != 0)
  906. return ret_val;
  907. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  908. ret_val = security_secctx_to_secid(
  909. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  910. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  911. &secid);
  912. if (ret_val != 0)
  913. return ret_val;
  914. return netlbl_unlhsh_add(&init_net,
  915. dev_name, addr, mask, addr_len, secid,
  916. &audit_info);
  917. }
  918. /**
  919. * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
  920. * @skb: the NETLINK buffer
  921. * @info: the Generic NETLINK info block
  922. *
  923. * Description:
  924. * Process a user generated STATICADDDEF message and add a new default
  925. * unlabeled connection entry. Returns zero on success, negative values on
  926. * failure.
  927. *
  928. */
  929. static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
  930. struct genl_info *info)
  931. {
  932. int ret_val;
  933. void *addr;
  934. void *mask;
  935. u32 addr_len;
  936. u32 secid;
  937. struct netlbl_audit audit_info;
  938. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  939. * single entry. However, allow users to create two entries, one each
  940. * for IPv4 and IPv6, with the same LSM security context which should
  941. * achieve the same result. */
  942. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  943. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  944. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  945. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  946. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  947. return -EINVAL;
  948. netlbl_netlink_auditinfo(skb, &audit_info);
  949. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  950. if (ret_val != 0)
  951. return ret_val;
  952. ret_val = security_secctx_to_secid(
  953. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  954. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  955. &secid);
  956. if (ret_val != 0)
  957. return ret_val;
  958. return netlbl_unlhsh_add(&init_net,
  959. NULL, addr, mask, addr_len, secid,
  960. &audit_info);
  961. }
  962. /**
  963. * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
  964. * @skb: the NETLINK buffer
  965. * @info: the Generic NETLINK info block
  966. *
  967. * Description:
  968. * Process a user generated STATICREMOVE message and remove the specified
  969. * unlabeled connection entry. Returns zero on success, negative values on
  970. * failure.
  971. *
  972. */
  973. static int netlbl_unlabel_staticremove(struct sk_buff *skb,
  974. struct genl_info *info)
  975. {
  976. int ret_val;
  977. char *dev_name;
  978. void *addr;
  979. void *mask;
  980. u32 addr_len;
  981. struct netlbl_audit audit_info;
  982. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  983. * IPv4 and IPv6 in the same entry. */
  984. if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
  985. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  986. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  987. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  988. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  989. return -EINVAL;
  990. netlbl_netlink_auditinfo(skb, &audit_info);
  991. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  992. if (ret_val != 0)
  993. return ret_val;
  994. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  995. return netlbl_unlhsh_remove(&init_net,
  996. dev_name, addr, mask, addr_len,
  997. &audit_info);
  998. }
  999. /**
  1000. * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
  1001. * @skb: the NETLINK buffer
  1002. * @info: the Generic NETLINK info block
  1003. *
  1004. * Description:
  1005. * Process a user generated STATICREMOVEDEF message and remove the default
  1006. * unlabeled connection entry. Returns zero on success, negative values on
  1007. * failure.
  1008. *
  1009. */
  1010. static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
  1011. struct genl_info *info)
  1012. {
  1013. int ret_val;
  1014. void *addr;
  1015. void *mask;
  1016. u32 addr_len;
  1017. struct netlbl_audit audit_info;
  1018. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  1019. * IPv4 and IPv6 in the same entry. */
  1020. if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  1021. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  1022. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  1023. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  1024. return -EINVAL;
  1025. netlbl_netlink_auditinfo(skb, &audit_info);
  1026. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  1027. if (ret_val != 0)
  1028. return ret_val;
  1029. return netlbl_unlhsh_remove(&init_net,
  1030. NULL, addr, mask, addr_len,
  1031. &audit_info);
  1032. }
  1033. /**
  1034. * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
  1035. * @cmd: command/message
  1036. * @iface: the interface entry
  1037. * @addr4: the IPv4 address entry
  1038. * @addr6: the IPv6 address entry
  1039. * @arg: the netlbl_unlhsh_walk_arg structure
  1040. *
  1041. * Description:
  1042. * This function is designed to be used to generate a response for a
  1043. * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
  1044. * can be specified, not both, the other unspecified entry should be set to
  1045. * NULL by the caller. Returns the size of the message on success, negative
  1046. * values on failure.
  1047. *
  1048. */
  1049. static int netlbl_unlabel_staticlist_gen(u32 cmd,
  1050. const struct netlbl_unlhsh_iface *iface,
  1051. const struct netlbl_unlhsh_addr4 *addr4,
  1052. const struct netlbl_unlhsh_addr6 *addr6,
  1053. void *arg)
  1054. {
  1055. int ret_val = -ENOMEM;
  1056. struct netlbl_unlhsh_walk_arg *cb_arg = arg;
  1057. struct net_device *dev;
  1058. void *data;
  1059. u32 secid;
  1060. char *secctx;
  1061. u32 secctx_len;
  1062. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
  1063. cb_arg->seq, &netlbl_unlabel_gnl_family,
  1064. NLM_F_MULTI, cmd);
  1065. if (data == NULL)
  1066. goto list_cb_failure;
  1067. if (iface->ifindex > 0) {
  1068. dev = dev_get_by_index(&init_net, iface->ifindex);
  1069. if (!dev) {
  1070. ret_val = -ENODEV;
  1071. goto list_cb_failure;
  1072. }
  1073. ret_val = nla_put_string(cb_arg->skb,
  1074. NLBL_UNLABEL_A_IFACE, dev->name);
  1075. dev_put(dev);
  1076. if (ret_val != 0)
  1077. goto list_cb_failure;
  1078. }
  1079. if (addr4) {
  1080. struct in_addr addr_struct;
  1081. addr_struct.s_addr = addr4->list.addr;
  1082. ret_val = nla_put(cb_arg->skb,
  1083. NLBL_UNLABEL_A_IPV4ADDR,
  1084. sizeof(struct in_addr),
  1085. &addr_struct);
  1086. if (ret_val != 0)
  1087. goto list_cb_failure;
  1088. addr_struct.s_addr = addr4->list.mask;
  1089. ret_val = nla_put(cb_arg->skb,
  1090. NLBL_UNLABEL_A_IPV4MASK,
  1091. sizeof(struct in_addr),
  1092. &addr_struct);
  1093. if (ret_val != 0)
  1094. goto list_cb_failure;
  1095. secid = addr4->secid;
  1096. } else {
  1097. ret_val = nla_put(cb_arg->skb,
  1098. NLBL_UNLABEL_A_IPV6ADDR,
  1099. sizeof(struct in6_addr),
  1100. &addr6->list.addr);
  1101. if (ret_val != 0)
  1102. goto list_cb_failure;
  1103. ret_val = nla_put(cb_arg->skb,
  1104. NLBL_UNLABEL_A_IPV6MASK,
  1105. sizeof(struct in6_addr),
  1106. &addr6->list.mask);
  1107. if (ret_val != 0)
  1108. goto list_cb_failure;
  1109. secid = addr6->secid;
  1110. }
  1111. ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
  1112. if (ret_val != 0)
  1113. goto list_cb_failure;
  1114. ret_val = nla_put(cb_arg->skb,
  1115. NLBL_UNLABEL_A_SECCTX,
  1116. secctx_len,
  1117. secctx);
  1118. security_release_secctx(secctx, secctx_len);
  1119. if (ret_val != 0)
  1120. goto list_cb_failure;
  1121. cb_arg->seq++;
  1122. return genlmsg_end(cb_arg->skb, data);
  1123. list_cb_failure:
  1124. genlmsg_cancel(cb_arg->skb, data);
  1125. return ret_val;
  1126. }
  1127. /**
  1128. * netlbl_unlabel_staticlist - Handle a STATICLIST message
  1129. * @skb: the NETLINK buffer
  1130. * @cb: the NETLINK callback
  1131. *
  1132. * Description:
  1133. * Process a user generated STATICLIST message and dump the unlabeled
  1134. * connection hash table in a form suitable for use in a kernel generated
  1135. * STATICLIST message. Returns the length of @skb.
  1136. *
  1137. */
  1138. static int netlbl_unlabel_staticlist(struct sk_buff *skb,
  1139. struct netlink_callback *cb)
  1140. {
  1141. struct netlbl_unlhsh_walk_arg cb_arg;
  1142. u32 skip_bkt = cb->args[0];
  1143. u32 skip_chain = cb->args[1];
  1144. u32 skip_addr4 = cb->args[2];
  1145. u32 skip_addr6 = cb->args[3];
  1146. u32 iter_bkt;
  1147. u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
  1148. struct netlbl_unlhsh_iface *iface;
  1149. struct list_head *iter_list;
  1150. struct netlbl_af4list *addr4;
  1151. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1152. struct netlbl_af6list *addr6;
  1153. #endif
  1154. cb_arg.nl_cb = cb;
  1155. cb_arg.skb = skb;
  1156. cb_arg.seq = cb->nlh->nlmsg_seq;
  1157. rcu_read_lock();
  1158. for (iter_bkt = skip_bkt;
  1159. iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
  1160. iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
  1161. iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
  1162. list_for_each_entry_rcu(iface, iter_list, list) {
  1163. if (!iface->valid ||
  1164. iter_chain++ < skip_chain)
  1165. continue;
  1166. netlbl_af4list_foreach_rcu(addr4,
  1167. &iface->addr4_list) {
  1168. if (iter_addr4++ < skip_addr4)
  1169. continue;
  1170. if (netlbl_unlabel_staticlist_gen(
  1171. NLBL_UNLABEL_C_STATICLIST,
  1172. iface,
  1173. netlbl_unlhsh_addr4_entry(addr4),
  1174. NULL,
  1175. &cb_arg) < 0) {
  1176. iter_addr4--;
  1177. iter_chain--;
  1178. goto unlabel_staticlist_return;
  1179. }
  1180. }
  1181. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1182. netlbl_af6list_foreach_rcu(addr6,
  1183. &iface->addr6_list) {
  1184. if (iter_addr6++ < skip_addr6)
  1185. continue;
  1186. if (netlbl_unlabel_staticlist_gen(
  1187. NLBL_UNLABEL_C_STATICLIST,
  1188. iface,
  1189. NULL,
  1190. netlbl_unlhsh_addr6_entry(addr6),
  1191. &cb_arg) < 0) {
  1192. iter_addr6--;
  1193. iter_chain--;
  1194. goto unlabel_staticlist_return;
  1195. }
  1196. }
  1197. #endif /* IPv6 */
  1198. }
  1199. }
  1200. unlabel_staticlist_return:
  1201. rcu_read_unlock();
  1202. cb->args[0] = skip_bkt;
  1203. cb->args[1] = skip_chain;
  1204. cb->args[2] = skip_addr4;
  1205. cb->args[3] = skip_addr6;
  1206. return skb->len;
  1207. }
  1208. /**
  1209. * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
  1210. * @skb: the NETLINK buffer
  1211. * @cb: the NETLINK callback
  1212. *
  1213. * Description:
  1214. * Process a user generated STATICLISTDEF message and dump the default
  1215. * unlabeled connection entry in a form suitable for use in a kernel generated
  1216. * STATICLISTDEF message. Returns the length of @skb.
  1217. *
  1218. */
  1219. static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
  1220. struct netlink_callback *cb)
  1221. {
  1222. struct netlbl_unlhsh_walk_arg cb_arg;
  1223. struct netlbl_unlhsh_iface *iface;
  1224. u32 skip_addr4 = cb->args[0];
  1225. u32 skip_addr6 = cb->args[1];
  1226. u32 iter_addr4 = 0;
  1227. struct netlbl_af4list *addr4;
  1228. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1229. u32 iter_addr6 = 0;
  1230. struct netlbl_af6list *addr6;
  1231. #endif
  1232. cb_arg.nl_cb = cb;
  1233. cb_arg.skb = skb;
  1234. cb_arg.seq = cb->nlh->nlmsg_seq;
  1235. rcu_read_lock();
  1236. iface = rcu_dereference(netlbl_unlhsh_def);
  1237. if (iface == NULL || !iface->valid)
  1238. goto unlabel_staticlistdef_return;
  1239. netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
  1240. if (iter_addr4++ < skip_addr4)
  1241. continue;
  1242. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1243. iface,
  1244. netlbl_unlhsh_addr4_entry(addr4),
  1245. NULL,
  1246. &cb_arg) < 0) {
  1247. iter_addr4--;
  1248. goto unlabel_staticlistdef_return;
  1249. }
  1250. }
  1251. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1252. netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
  1253. if (iter_addr6++ < skip_addr6)
  1254. continue;
  1255. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1256. iface,
  1257. NULL,
  1258. netlbl_unlhsh_addr6_entry(addr6),
  1259. &cb_arg) < 0) {
  1260. iter_addr6--;
  1261. goto unlabel_staticlistdef_return;
  1262. }
  1263. }
  1264. #endif /* IPv6 */
  1265. unlabel_staticlistdef_return:
  1266. rcu_read_unlock();
  1267. cb->args[0] = skip_addr4;
  1268. cb->args[1] = skip_addr6;
  1269. return skb->len;
  1270. }
  1271. /*
  1272. * NetLabel Generic NETLINK Command Definitions
  1273. */
  1274. static struct genl_ops netlbl_unlabel_genl_ops[] = {
  1275. {
  1276. .cmd = NLBL_UNLABEL_C_STATICADD,
  1277. .flags = GENL_ADMIN_PERM,
  1278. .policy = netlbl_unlabel_genl_policy,
  1279. .doit = netlbl_unlabel_staticadd,
  1280. .dumpit = NULL,
  1281. },
  1282. {
  1283. .cmd = NLBL_UNLABEL_C_STATICREMOVE,
  1284. .flags = GENL_ADMIN_PERM,
  1285. .policy = netlbl_unlabel_genl_policy,
  1286. .doit = netlbl_unlabel_staticremove,
  1287. .dumpit = NULL,
  1288. },
  1289. {
  1290. .cmd = NLBL_UNLABEL_C_STATICLIST,
  1291. .flags = 0,
  1292. .policy = netlbl_unlabel_genl_policy,
  1293. .doit = NULL,
  1294. .dumpit = netlbl_unlabel_staticlist,
  1295. },
  1296. {
  1297. .cmd = NLBL_UNLABEL_C_STATICADDDEF,
  1298. .flags = GENL_ADMIN_PERM,
  1299. .policy = netlbl_unlabel_genl_policy,
  1300. .doit = netlbl_unlabel_staticadddef,
  1301. .dumpit = NULL,
  1302. },
  1303. {
  1304. .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
  1305. .flags = GENL_ADMIN_PERM,
  1306. .policy = netlbl_unlabel_genl_policy,
  1307. .doit = netlbl_unlabel_staticremovedef,
  1308. .dumpit = NULL,
  1309. },
  1310. {
  1311. .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
  1312. .flags = 0,
  1313. .policy = netlbl_unlabel_genl_policy,
  1314. .doit = NULL,
  1315. .dumpit = netlbl_unlabel_staticlistdef,
  1316. },
  1317. {
  1318. .cmd = NLBL_UNLABEL_C_ACCEPT,
  1319. .flags = GENL_ADMIN_PERM,
  1320. .policy = netlbl_unlabel_genl_policy,
  1321. .doit = netlbl_unlabel_accept,
  1322. .dumpit = NULL,
  1323. },
  1324. {
  1325. .cmd = NLBL_UNLABEL_C_LIST,
  1326. .flags = 0,
  1327. .policy = netlbl_unlabel_genl_policy,
  1328. .doit = netlbl_unlabel_list,
  1329. .dumpit = NULL,
  1330. },
  1331. };
  1332. /*
  1333. * NetLabel Generic NETLINK Protocol Functions
  1334. */
  1335. /**
  1336. * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
  1337. *
  1338. * Description:
  1339. * Register the unlabeled packet NetLabel component with the Generic NETLINK
  1340. * mechanism. Returns zero on success, negative values on failure.
  1341. *
  1342. */
  1343. int __init netlbl_unlabel_genl_init(void)
  1344. {
  1345. int ret_val, i;
  1346. ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
  1347. if (ret_val != 0)
  1348. return ret_val;
  1349. for (i = 0; i < ARRAY_SIZE(netlbl_unlabel_genl_ops); i++) {
  1350. ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
  1351. &netlbl_unlabel_genl_ops[i]);
  1352. if (ret_val != 0)
  1353. return ret_val;
  1354. }
  1355. return 0;
  1356. }
  1357. /*
  1358. * NetLabel KAPI Hooks
  1359. */
  1360. static struct notifier_block netlbl_unlhsh_netdev_notifier = {
  1361. .notifier_call = netlbl_unlhsh_netdev_handler,
  1362. };
  1363. /**
  1364. * netlbl_unlabel_init - Initialize the unlabeled connection hash table
  1365. * @size: the number of bits to use for the hash buckets
  1366. *
  1367. * Description:
  1368. * Initializes the unlabeled connection hash table and registers a network
  1369. * device notification handler. This function should only be called by the
  1370. * NetLabel subsystem itself during initialization. Returns zero on success,
  1371. * non-zero values on error.
  1372. *
  1373. */
  1374. int __init netlbl_unlabel_init(u32 size)
  1375. {
  1376. u32 iter;
  1377. struct netlbl_unlhsh_tbl *hsh_tbl;
  1378. if (size == 0)
  1379. return -EINVAL;
  1380. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  1381. if (hsh_tbl == NULL)
  1382. return -ENOMEM;
  1383. hsh_tbl->size = 1 << size;
  1384. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  1385. sizeof(struct list_head),
  1386. GFP_KERNEL);
  1387. if (hsh_tbl->tbl == NULL) {
  1388. kfree(hsh_tbl);
  1389. return -ENOMEM;
  1390. }
  1391. for (iter = 0; iter < hsh_tbl->size; iter++)
  1392. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  1393. rcu_read_lock();
  1394. spin_lock(&netlbl_unlhsh_lock);
  1395. rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
  1396. spin_unlock(&netlbl_unlhsh_lock);
  1397. rcu_read_unlock();
  1398. register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
  1399. return 0;
  1400. }
  1401. /**
  1402. * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
  1403. * @skb: the packet
  1404. * @family: protocol family
  1405. * @secattr: the security attributes
  1406. *
  1407. * Description:
  1408. * Determine the security attributes, if any, for an unlabled packet and return
  1409. * them in @secattr. Returns zero on success and negative values on failure.
  1410. *
  1411. */
  1412. int netlbl_unlabel_getattr(const struct sk_buff *skb,
  1413. u16 family,
  1414. struct netlbl_lsm_secattr *secattr)
  1415. {
  1416. struct netlbl_unlhsh_iface *iface;
  1417. rcu_read_lock();
  1418. iface = netlbl_unlhsh_search_iface_def(skb->iif);
  1419. if (iface == NULL)
  1420. goto unlabel_getattr_nolabel;
  1421. switch (family) {
  1422. case PF_INET: {
  1423. struct iphdr *hdr4;
  1424. struct netlbl_af4list *addr4;
  1425. hdr4 = ip_hdr(skb);
  1426. addr4 = netlbl_af4list_search(hdr4->saddr,
  1427. &iface->addr4_list);
  1428. if (addr4 == NULL)
  1429. goto unlabel_getattr_nolabel;
  1430. secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
  1431. break;
  1432. }
  1433. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1434. case PF_INET6: {
  1435. struct ipv6hdr *hdr6;
  1436. struct netlbl_af6list *addr6;
  1437. hdr6 = ipv6_hdr(skb);
  1438. addr6 = netlbl_af6list_search(&hdr6->saddr,
  1439. &iface->addr6_list);
  1440. if (addr6 == NULL)
  1441. goto unlabel_getattr_nolabel;
  1442. secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
  1443. break;
  1444. }
  1445. #endif /* IPv6 */
  1446. default:
  1447. goto unlabel_getattr_nolabel;
  1448. }
  1449. rcu_read_unlock();
  1450. secattr->flags |= NETLBL_SECATTR_SECID;
  1451. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1452. return 0;
  1453. unlabel_getattr_nolabel:
  1454. rcu_read_unlock();
  1455. if (netlabel_unlabel_acceptflg == 0)
  1456. return -ENOMSG;
  1457. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1458. return 0;
  1459. }
  1460. /**
  1461. * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
  1462. *
  1463. * Description:
  1464. * Set the default NetLabel configuration to allow incoming unlabeled packets
  1465. * and to send unlabeled network traffic by default.
  1466. *
  1467. */
  1468. int __init netlbl_unlabel_defconf(void)
  1469. {
  1470. int ret_val;
  1471. struct netlbl_dom_map *entry;
  1472. struct netlbl_audit audit_info;
  1473. /* Only the kernel is allowed to call this function and the only time
  1474. * it is called is at bootup before the audit subsystem is reporting
  1475. * messages so don't worry to much about these values. */
  1476. security_task_getsecid(current, &audit_info.secid);
  1477. audit_info.loginuid = 0;
  1478. audit_info.sessionid = 0;
  1479. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1480. if (entry == NULL)
  1481. return -ENOMEM;
  1482. entry->type = NETLBL_NLTYPE_UNLABELED;
  1483. ret_val = netlbl_domhsh_add_default(entry, &audit_info);
  1484. if (ret_val != 0)
  1485. return ret_val;
  1486. netlbl_unlabel_acceptflg_set(1, &audit_info);
  1487. return 0;
  1488. }