netlabel_unlabeled.c 43 KB

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